JQuery (1)--jquery prequel JavaScript101

Source: Internet
Author: User

What is jquery, what can be done, the network on the article is a dime, the author no longer discuss these issues. I just want to talk about how jquery is implemented, (explore his code, learn about other people's thinking).

The jquery code is very elegant, that's for sure. jquery uses a lot of JavaScript language skills, but if we don't know much about JavaScript itself, then read jquery, That would be quite difficult. So I would like to talk about JavaScript before I introduce the way of jquery implementation.

Very good articles on JavaScript web, but also a lot, but I still highly recommend the blog Li Shi article, especially the first half (of course, the second half is more exciting, but it is not the basics of JavaScript), its JavaScript Foundation and theory The discussion is farsighted.

There are such excellent articles in front of, if I continue to discuss the same problem, it is a bit of a fish, hardline officials. But the author for Li Shi not mentioned a few JavaScript problems, or want to add a statement, even if it is "Dog" bar.

Use of function in 1.JavaScript

In general, function has two uses in JavaScript, A. Represents a common method and B. Represents a "type". If the normal method is represented, it can be invoked in other objects by the name of the function, if the expression is a type, To invoke it, you need to use the new operator to get an instance (of course, if you use this type of "static method", it needs to be directly using this type of name). If you can understand these two uses of function, then you will not confuse the JavaScript function of "This", who is the point: a. If it represents a normal method, then this in function refers to The object that invokes this function (that is, the caller); if it represents a type, then this in function represents the object that is currently instantiated by the new function.

"Static members" and "instance members" of types in 2.JavaScript

If a function represents a "type", it can have both "static members" and "instance members."

Let's look at the code first.

var xieRan = function() {
      this.userName = "xieran";
      this.getName = function() {
        return "my name is " + this.userName;
      };
    }

In this code, I define a "type"--xieran, within which we define a property (UserName) and a method (GetName), and the two members, we say, are "instance members" because we can pass the new After a type gets the instance, it is invoked on it, as follows:

var myInstance = new xieRan();
    myInstance.getName();
    myInstance.userName;

This said "instance member", Next we say "static member", we know the JavaScript language is quite flexible, it can be in the type of "external", the corresponding member "append" to the type, as follows (note: Here the "external" word)

var xieRan = function() {
      this.userName = "xieran";
      this.getName = function() {
        return "my name is " + this.userName;
      };
    };

xieRan.location = "qindao";
xieRan.getLoacation = function() {
   return "qingdao City";
};

In this way, when we use Loacation and getloacation (), we can call directly through the name of the type (without, at the same time, not a new instance to invoke these members), as follows:

xieRan.location;
xieRan.getLoacation();

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.