jquery - Variable as a function at the same time in JavaScript -


i want implement this.

myvar.myanothervar; myvar.mymethod(); myvar("sample text"); 

and how jquery implemented it

jquery.fn; jquery.ajax(); jquery("#mybtn"); 

how can implement jquery holds on single namespace? prototype? how can variable can used invoke function @ same time?

thank :)

functions in javascript objects, can arbitrarily add properties them other object:

function foo() {     // code when call `foo` goes here } foo.somedata = "bar"; foo.somefunction = function() {     // code when call `foo.somefunction` goes here }; 

what jquery more complicated, , bit unusual, in couple of ways:

  1. the jquery function (usually aliased $) wrapper around call constructor function creates new object. (the constructor function called init.)

  2. they've added reference init function's prototype property in property on jquery function called fn. both properties (jquery.fn , init.prototype) refer object become instance's prototype when use new init.

  3. they've also referred same object jquery.prototype, though jquery isn't used constructor function. $() instanceof $ true.

but don't have things you've described. can, of course, don't have to.

if want 2 things, basic structure looks this:

var foo = function() {     // public function     function foo(args, here) {         return new init(args, here);     }      // hidden constructor function     function init(args, here) {         // ...do `this`, perhaps     }      // object becomes prototype of instances created via `new init`     foo.fn = foo.prototype = init.prototype = {         // instance-specific function, jquery's `css`         instancemethod: function() {             // has access `this`         }     };      // random piece of "static" (not instance-specific) data     foo.somedata = "bar";      // "static" (not instance-specific) function, jquery's `$.ajax`     foo.staticmethod = function() {         // doesn't have (useful) access `this`     };      return foo; }(); 

Comments

Popular posts from this blog

facebook - android ACTION_SEND to share with specific application only -

python - Creating a new virtualenv gives a permissions error -

javascript - cocos2d-js draw circle not instantly -