How jquery is structured. jquery is an architecture

Source: Internet
Author: User

How jquery is structured. jquery is an architecture

I have always had a question.

Jquery can be used as a method, for example, $ (), or $. extend () for an object ();

Let's summarize it now.

Function method () {} var m = new method (); m. version = "123"; console. log (m. version) // print 123

After declaring an object, you can add attributes or methods to the object.

Continue to look down:

var m=function(){};m.version="123";

This area can still print 123;

In this lesson, no object is stated, but a variable method name is defined.

Core 1: in fact, the variable method name is also an object

For example, the above Code is essentially like this.

var m=new Function();m.version="123";

This is why jQuery can directly sit on objects.

Next, let's see why jQuery can be used directly.

Core 2:If the constructor returns a value, the object created by the new operator is discarded. The returned value is the value of the new expression.

Function Method (name, age) {var obj = {name: name, age: age };
This. name = "I am from myself"; return obj;} var objM = new Method ("Dr. Sisi", 26); console. log (objM); // print the obj object without printing "I'm from myself"

With the above two core theories, we can build jQuery.

(Function (window, undefined) {var jQuery = (function (){
// Core theory 2 var jQuery = function (name, age) {return new jQuery. prototype. init (name, age) ;}; jQuery. prototype = {init: function (name, age) {return [name, age] ;}}; jQuery. prototype. init. prototype = jQuery. prototype;
// Core theory 1. jQuery. version = "1.7.2"; return jQuery;}) (); window. $ = window. jQuery = jQuery;}) (window );

Console. log (jQuery. version );
Console. log (jQuery ("Dr. Sisi", 25 ))

The above code imitates jQuery as a method and the basic theory used for objects.

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.