A brief talk on Java script oop--

Source: Internet
Author: User

First, the detailed

1, who finally call the function, this is pointing to WHO. (Specific as follows:)

①this point, it can never be an object!
②this point to WHO, never depends on where this is written!! Instead, it depends on where the function is called.
③this refers to the object that we call the context of the function, also called the function's caller.

2, this is the direction of the law (a total of six this is the focus)

① is called directly through the function name (): This points to the window

function func (){

Console.log (this);
}
Func ();


② is called by the object. Function name (): This points to this object

var obj = {
Name: "obj",
Func1:func
};
Obj.func1 ();


The ③ function acts as an element of an array, called by an array subscript: This points to this array

var arr = [Func,1,2,3];
Arr[0] (); This--->arr


The ④ function is called as a callback function for the window's built-in function: This points to window setinterval setTimeout, etc...

SetTimeout (func,+);//This--->window
SetInterval (func,1000);


⑤ function as a constructor, when called with the New keyword: This points to the newly-created object

var obj = new func ();//this--->new


⑥ is called by call, apply, and bind, which points to the object we specify. (Human-controlled)
Func.call (obj, parameter 1, parameter 2, Parameter 3 ....) )
Func.apply (obj, parameter 1, parameter 2, Parameter 3 ....) )
Func.bind (obj, parameter 1, parameter 2, Parameter 3 ....) ) var f = function.bind (obj)

second, the prototype chain      

1. The _proto_ of the new object is pointed to by the constructor
2. _proto_ of all functions points to function () prototype of built-in functions
3, non-constructor new objects include ({} newObject () object prototype) _proto_ point to Object prototype
4, the _proto_ of object points to null

The following is a self-drawn prototype chain diagram

Three, closed package

the scope in JS

1 . Global variables: Variables declared outside the function

Local variables: variables declared within a function

In JS, the function is a unique local scope, and if, for, and other {} have no scope of their own
Therefore, the local variable cannot be accessed outside the function. In fact, after the function is executed, the memory is freed.

2. How do I access function private variables?
JS, provides a "closure" concept: Inside the function, define a child function, you can use child functions to access the parent function of the private variables. After the operation is done, the child function is returned by return.
function Func2(){
varNum= 1;
function func3(){
varSum=Num+Ten;
alert (sum);
}
returnfunc3;
}

varF=Func2 ();
f ();

3, the role of closures:
① the private variable of the access function;
② allows a function's variable to always exist in memory without being freed.

A brief talk on Java script oop--

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.