반응형
In jQuery, the fn property is just an alias to the prototype property.
The jQuery identifier (or $) is just a constructor function, and all instances created with it, inherit from the constructor's prototype.
A simple constructor function:
function Test() {
this.a = 'a';
}
Test.prototype.b = 'b';
var test = new Test();
test.a; // "a", own property
test.b; // "b", inherited propertyA simple structure that resembles the architecture of jQuery:
(function() {
var foo = function(arg) { // core constructor
// ensure to use the `new` operator
if (!(this instanceof foo))
return new foo(arg);
// store an argument for this example
this.myArg = arg;
//..
};
// create `fn` alias to `prototype` property
foo.fn = foo.prototype = {
init: function () {/*...*/}
//...
};
// expose the library
window.foo = foo;
})();
// Extension:
foo.fn.myPlugin = function () {
alert(this.myArg);
return this; // return `this` for chainability
};
foo("bar").myPlugin(); // alerts "bar"
반응형
'IT개발 > Javascript' 카테고리의 다른 글
| [Javascript] JSON 정리 (0) | 2014.01.23 |
|---|---|
| [Jquery] 브라우저 기능 플래그 ( 브라우저 별 개발 시 ) (0) | 2014.01.23 |
| [Javascript]Fire fox firebug 플러그인 설치 (0) | 2014.01.23 |
| [Javascript] 클로저(closure) 란? (0) | 2014.01.23 |
| [Javascript] 콜백 함수란(CALLBACK Function)? (0) | 2014.01.23 |