As an example, here is a screenshot from jQuery1.8.

Why can I do something like this from the perspective of jQuery source code ?
Is addClass jquery’s method? But what i saw was from jQuery.fn.extend.
$("xid").addClass("xxx");
I am relatively new to JavaScript and wrote some pretty simple code like directly add a method in .html.
<html>
<script>
function m() {
alert("simple");
}
</script>
</html>
I couldn’t understand the complex syntax particularly in the JavaScript Framework like ExtJs,jQuery, although I have searched a number of relative documents about this, but I still don’t have a clear understanding. Maybe I need to spend time seriously reading a special JavaScript book, but this moment I want to know the answer.
Thanks.
There are several layers to this question. The first is object literals. Javascript allows you to declare objects using object literal syntax (you may have heard of JSON which is just the syntax stored/transferred in a string). The syntax is as follows:
A more structured/readable way to write this is:
The code above is exactly the same as doing:
So what you’re seeing in the line:
is merely a part of the object literal declaration. Essentially the code is doing this:
Except that it declares the object literal directly in the call to
extend()so it is not necessary to use a temporary variable.The second thing is functions in javascript are not special. What I mean is, functions are treated the same as everything else: strings, numbers, arrays & objects. In fact, in javascript, strings, numbers, arrays and functions are all objects. So you can assign a function to a variable:
So there is nothing magical happening at all. It’s simply creating an anonymous object in object literal syntax that contains a collection of functions that is passed as an argument to the
extendmethod.That is the explanation of the syntax. That is, what is happening in javascript per se.
We know that we’re looking at jQuery code. So that gives us more clues as to what’s happening. First thing first:
jQuery.fnis simply a pointer tojQuery.prototype. AndjQueryitself is a constructor. In javascript constructors are regular functions that can be used to create objects. When you create an object with a constructor, that object inherits from the constructor’s prototype.And jQuery has modified its prototype and added an
extendmethod which allows you to add other methods and attributes to jQuery’s prototype. Which is what it’s doing. All the functions contained in the object literal mentioned earlier will be processed by theextendmethod to be added to jQuery itself so that when you create new jQuery objects they’ll inherit them.Based on your comment it looks like there are lots of things about javascript you need to learn. May I suggest reading through the Rhino book: “JavaScript: The Definitive Guide”. And once you know more and are more comfortable with javascript I’d also suggest reading “Javascript: The Good Parts”.
I’ll try to answer the best I can here about your comment. To explain everything requires a whole book.
First, about the line:
As mentioned before,
jQuery.fnis just a pointer tojQuery.prototype. So what it’s doing is basically this:What’s the difference between the two? The first thing you need to remember is that
jQueryis a constructor. An object that is created by a constructor inherits it’s prototype (javascript don’t have classes but switch the word “prototype” with “class” and you begin to get an understanding of how objects work). So, the first declaration of extend defines it as a method for all instances ofjQueryobjects:Now, remember I said that in javascript functions are also objects? The second declaration treats the
jQueryfunction as an object and adds a function directly to it. This is mostly syntax sugar. It’s so that you can write:instead of the longer:
or something convoluted like:
depending on how extend was written.
So,
jQuery.extend()is simply a method of the jQuery constructor itself. It is not inherited by objects created by the jQuery constructor. Think of it as a class method.