admin管理员组文章数量:1310385
To organize my code, I happened to write as namespace for my main javascript file. Then I want to call some of the functions of that file to my custom javascript file, let say script.js
. The problem is that I couldn't access the methods of the namespace. Here is my example code:
main.js
$( function() {
"use strict"
var Accordian = {
slide : function() {
$('h3').click( function() {
$(this).next('div').slideToggle('1000');
$(this).toggleClass('toggled');
});
},
slideEaseOutBounce: function() {
$('h3').click( function() {
$(this).next().animate(
{'height' : 'toggle'}, 1000, 'easeOutBounce'
);
$(this).toggleClass('toggled');
});
},
slideEaseInOutExpo: function() {
$('h3').click( function() {
$(this).next().animate(
{'height' : 'toggle'}, 1000, 'easeInOutExpo'
);
$(this).toggleClass('toggled');
});
}
});
And I have tried as in below script.js
$(document).ready( function() {
Accordian.slide();
});
UPDATED:
Here's the link: .html
And the error message occurs "ReferenceError: Accordian is not defined"
Any help would be very much appreciated.
To organize my code, I happened to write as namespace for my main javascript file. Then I want to call some of the functions of that file to my custom javascript file, let say script.js
. The problem is that I couldn't access the methods of the namespace. Here is my example code:
main.js
$( function() {
"use strict"
var Accordian = {
slide : function() {
$('h3').click( function() {
$(this).next('div').slideToggle('1000');
$(this).toggleClass('toggled');
});
},
slideEaseOutBounce: function() {
$('h3').click( function() {
$(this).next().animate(
{'height' : 'toggle'}, 1000, 'easeOutBounce'
);
$(this).toggleClass('toggled');
});
},
slideEaseInOutExpo: function() {
$('h3').click( function() {
$(this).next().animate(
{'height' : 'toggle'}, 1000, 'easeInOutExpo'
);
$(this).toggleClass('toggled');
});
}
});
And I have tried as in below script.js
$(document).ready( function() {
Accordian.slide();
});
UPDATED:
Here's the link: http://jsnamespace.yr./using-accordian.html
And the error message occurs "ReferenceError: Accordian is not defined"
Any help would be very much appreciated.
- When I try and access the site I get the following, followed by some parking ads: This website was set to be removed for inactivity by www.000webhost.. If you own this website, click here to protect it. About your question, are you simply referencing both scripts in your HTML file or is there another method that you're using? – Qantas 94 Heavy Commented Jun 3, 2013 at 13:27
- Ah, this link is just removed. I added 3 days ago. I'll uploaded it again. Here it works again. jsnamespace.yr./using-accordian.html – Tepken Vannkorn Commented Jun 3, 2013 at 15:58
3 Answers
Reset to default 4 +50Note: line 63
is missing a semicolon in script.js
.
Since modifying your site to use this code works:
<script type="text/javascript">
$(document).ready( function() {
Accordian[0].slide();
});
</script>
FIDDLE DEMO
it seems that wrapping your "Accoridian" inside the jQuery function requires it to be dereferenced as a sub element.
When you remove the $
at line 1
of script.js
turning
var Accordian = $(function () {
into
var Accordian = (function () {
(like @anonyme suggested) it works with your original call to Accordian.slide()
.
first you have to make a unique root namespace like that :
window.Accordian = window.Accordian || {};
This part must be done on top of all the next ! it Can be done in the head of html page in a script tag.
after you can use your object :
//creating a function :
Accordian.slide = function() {
$('h3').click( function() {
$(this).next('div').slideToggle('1000');
$(this).toggleClass('toggled');
});
}
//and later using it :
Accordian.slide()
This is the simplest way !
You prefered using multiple file, so let's try :
mains.js
/*$*/( function(globalAccordian) {
"use strict"
/* var Accordian = { */
globalAccordian = {
// doing like this will override your globalAccordian
// if it was already generated by another script
slide : function() {
$('h3').click( function() {
$(this).next('div').slideToggle('1000');
$(this).toggleClass('toggled');
});
},
slideEaseOutBounce: function() {
$('h3').click( function() {
$(this).next().animate(
{'height' : 'toggle'}, 1000, 'easeOutBounce'
);
$(this).toggleClass('toggled');
});
},
slideEaseInOutExpo: function() {
$('h3').click( function() {
$(this).next().animate(
{'height' : 'toggle'}, 1000, 'easeInOutExpo'
);
$(this).toggleClass('toggled');
});
}
})( window.Accordian || {} );
script.js
$(document).ready( function() {
Accordian.slide();
})
You can't access the namespace because it is encapsulated in a function. I'd suggest reading up on the revealing module pattern.
JSFiddle: http://jsfiddle/R927K/
Example:
var Accordian = (function() {
var Accordian = {
slide : function() {
alert('sliding');
}
};
return Accordian;
}());
Accordian.slide();
本文标签: jqueryCalling a method from a namespace to use in another javascript fileStack Overflow
版权声明:本文标题:jquery - Calling a method from a namespace to use in another javascript file - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741816813a2399124.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论