JS function, Object, instance method, object method understand __ function

Source: Internet
Author: User
Tags anonymous instance method object object prototype definition


var _s = function () {var name = ' Lisa '; Internal properties
var age = 10;
var sen = function () {
Console.log ("function _s"); function Internal property, scope only inside function,
}
return "1";
}
function _e () {
name = ' Lisa ';
Age = 10;
Sen = function () {
Console.log ("function _e");
}
}
var person=function () {
this.sn = ' Sam '; Instance properties, instance methods
};
Person.say=function () {//static method
Console.log (' I am a person,i can say. ')
};
var person1 = new Person ();
Person1.getname=function (name) {//instance method
Console.log (' My name is ' +name);
}
Person.prototype.gets = function () {//instance method, property or function defined by functional prototype property can be shared by all instance objects of the function.
Console.log ("prototype method");
}
Console.log (WINDOW.SN); Normal function internal this points to the current Window object, so the assignment is also
When the new function object is instantiated, the this point of the function property is changed, this is changed from the original point to the Window object to the current instance object
Console.log (new Person (). sn); Once instantiated, this points to the current object, so the current object has the SN attribute
Console.log (new Person (). gets ());
Console.log (New Person ());

//When an instance method of the same name is defined on an instance, on a prototype reference, and on "This", the instance takes precedence over that one?

Var baseclass = function ()  {     this.method1 = function () {             alert ('  Defined by the  ') This " in the instance method ');      }    };      Var instance1 = new baseclass ();     Instance1.method1 = function () {         alert ('  Defined  directly in the instance method ');    }     Baseclass.prototype.method1 = function () {         alert ('  Defined by the prototype instance method  ');    }      instance1.method1 ();//defined directly in the instance method    &nbsP

By running the result tracking test, you can see that the variables defined directly on the instance are higher than those defined on "this", and those defined on "this" are higher than the prototype definition. That is, a variable that is defined directly on the instance overrides the variable defined on "This" and prototype, and defines the variable that overrides the prototype definition on "this"


Summary: JS static method or static properties can only be called by the JS function (understood as a class), instance methods or instance properties can only be invoked by the JS instance object. ======================================================
function Instantiation Process:
constructor does not need to display the return value. When you use new to create an object (call a constructor), if the return is a non object (number, String, Boolean type, and so on), it returns a value, and returns the object if it is an object.
The following is a brief introduction to the process of the new object in javascript: such as var myobj = Newperson ("Aty", 25);
1. Create an empty object object. var obj = new Object ();
2. In the constructor person, this points to the Obj object you just created
3. The __proto__ of obj to be created points to the prototype of the constructor person. This step is to establish a direct correspondence between objects and prototypes. Firefox through
The __proto__ property of the object can access the prototype, IE does not expose the corresponding attribute.
4. Execute the code in the constructor person ()


The JS function itself is a constructor, which is the same as the anonymous constructor function, but the function requires new to produce an anonymous function.
What does the new operator do? In fact, it's very simple, it's done three things.
function Base () {}
var obj = new Base ();
var obj = {};
obj.__proto__ = Base.prototype;
Base.call (obj); Very important: Change the point of this object to
/* The first line, we created an empty object obj
In the second line, we point the __proto__ member of this empty object to the base function object prototype member object
In the third line, we replace the this pointer of the base function object with obj, and then call the base function, so we assign an ID member variable to the Obj object, and the value of the member variable is "base", about the use of the called function. */


function constructor, new function to create anonymous functions
var baseclass = new Function;
var Class2 = BaseClass;
BASECLASS.F1 = function () {
Console.log ("BaseClass ' s static method");
}
CLASS2.F2 = function () {
Console.log ("Class2 ' s static method");
}
Baseclass.f1 ();//baseclass ' s static method
Baseclass.f2 ();//class2 ' s static method
Class2.f1 ();//baseclass ' s static method
Class2.f2 ();//class2 ' s static method

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.