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.

Share Improve this question asked Sep 11, 2011 at 15:07 Kevin PeiKevin Pei 5,8727 gold badges40 silver badges55 bronze badges 2
  • 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
Add a ment  | 

4 Answers 4

Reset to default 11

Functions 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