admin管理员组文章数量:1241151
I'm trying to create a javascript framework by myself(so no jquery,mootools... code please) and I want to make the code for my framework only accessible in a framework function so for example, something like this:
frameworkname({
//framework code here
});
so my framework code doesn't conflict with other frameworks. I know that frameworkname({});
is a function, but I don't know how you pass code as a function argument. I know this is possible as I'm quite experienced in jquery and jquery has that stuff everywhere (example:$(document).ready(function(){//codehere});
), but how did the jquery developers do this? I want to be able to do this for my framework. Any help is greatly appreciated.
I'm trying to create a javascript framework by myself(so no jquery,mootools... code please) and I want to make the code for my framework only accessible in a framework function so for example, something like this:
frameworkname({
//framework code here
});
so my framework code doesn't conflict with other frameworks. I know that frameworkname({});
is a function, but I don't know how you pass code as a function argument. I know this is possible as I'm quite experienced in jquery and jquery has that stuff everywhere (example:$(document).ready(function(){//codehere});
), but how did the jquery developers do this? I want to be able to do this for my framework. Any help is greatly appreciated.
- 3 Can you not just look at the jQuery source? – m.edmondson Commented Sep 11, 2011 at 15:11
- Oh boy, now that's a misconception... I begin to sympathize with the people plaining about using jQuery for everything. – user395760 Commented Sep 11, 2011 at 15:13
4 Answers
Reset to default 11Functions are first class objects in JavaScript. You can pass them around in the same way as Strings, Numbers, Arrays or any other data type.
var foo = function () { alert(1); };
var bar = function (arg) { arg(); }
bar(foo);
In JavaScript, variables can represent any data type. Therefore, in function frameworkname( x ) { }
can take an argument of any type (function, object, primitive, etc).
function frameworkname( x ) {
if ( typeof x === "function" ) {
x(); // function passed
} else if ( typeof x === "object" ) {
for ( var i in x ) {
// Object passed
x[i]();
}
}
}
frameworkname(function() {
alert("function passed as argument");
});
frameworkname({
hello: function() {
alert("hello");
}
});
in javascript, a function is exactly a variable, the code you mentioned:
$(document).ready( function () {//code});
is in fact declared a function without a name, and then passed it as an argument to "ready()". Thus, your mind of pass code to function in the way
frameworkname( {} );
is illegal because "{}" is a code block but not any kind of "variable"
I think something like this:
function frameworkname(){
this.ready = function(code){
code();
};
return this;
}
frameworkname().ready(function(){
console.log('Function executed');
});
本文标签: frameworksPass code as function argument in javascriptStack Overflow
版权声明:本文标题:frameworks - Pass code as function argument in javascript? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1740002384a2219647.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论