1
var jQuery = function( selector ) {
    return new jQuery.fn.init( selector );
};

jQuery.fn = jQuery.prototype = {...};

var init = jQuery.fn.init = function( selector ){
    this.selector   = selector;
    this.element    = document.querySelector(this.selector);
};

console.log(
    jQuery('div').selector
);

I'm currently study how jQuery works, but I got few questions

  1. jQuery.fn = jQuery.prototype = {}; why prototype = object, isn't prototype usually come with name after jQuery.prototype.foo = function...

  2. var init = jQuery.fn.init (jQuery.prototoype.init) when I remove var init, i got an error, like this : var jQuery.fn.init = ...

0

2 Answers 2

1

1) jQuery.fn = jQuery.prototype = {}; why prototype = object

Actually, prototype is an object only when you do something like,

**jQuery.prototype.get = function(){
 //Code
}**

You are actually creating a member function in prototype object only with name "get". You could have done it like this also as:

**jQuery.prototype = {
  "get" : function(){
  }
}**

2) When you remove init, the statement becomes as var jQuery.fn.init = ...

Actually this is not a valid statement because jQuery object is already available. It is a syntactical error. You cannot create a member of any object like this. For that, you just need to do as

**jQuery.fn.init = function(){
}**  
Sign up to request clarification or add additional context in comments.

1 Comment

is jQuery.fn.init = jQuery.prototype.init? is this relate to jQuery.prototype ={} object?
1
  1. Setting the prototype to be an empty object clears every left-over from the JavaScript side and makes it a completely new "class" or object.

  2. When you remove the var-part, you basically remove the complete definition. A variable name cannot contain dots, because a dot by definition requires an object. Therefore, var a.b.c = 1 will not be valid. You need to have an object a = { b: {} } and then do a.b.c = 1.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.