JavaScript constructors. Prototype prototype understanding function. Call () understanding of method cloning

Source: Internet
Author: User

Creating object objects directly

var New Object (); var = {};stu.name= "Zhangsan"="stu"function() {  alert ( "My name Zhang San");  }

You can also use the Stu as a module, the module has a number of objects, the following example for a simple shopping cart system module!

Shoppingcar.carinfo = function (obj) {
Shoppingcar.carinfo . Carnumber = Obj.carnumber;
.....
}//Shopping Cart Object
Shoppingcar.orderinfo = function (obj) {
Shoppongcar.orderinfo.   ordernumber = obj.ordernumber;
...
}//Order Object

Call
Carinfo = {
Carnumber: "10000"
}
Shoppingcar.carinfo (Carinfo);

What does prototype mean?

Each object in JavaScript has the prototype property, and the prototype property of the object in JavaScript is interpreted as a reference to the object type prototype.

A.prototype = new B ();

Understanding prototype should not confuse it with inheritance. A's prototype is an example of B, and it's understandable that a will clone the methods and properties in B all over again. A can use the methods and properties of B. The emphasis here is on cloning, not inheritance. This can happen: the prototype of A is an instance of B, and the prototype of B is also an instance of a.

It's very useful to have someone else's notes.

1. Reference types in the prototype chain.
2. Prototype chain do not rely on each other, the GC is not so kind to help you create a temporary storage area.

1 function base () {
This.arr = [];
}
function sub () {

}
Sub.prototype= new Base ();
var a = new sub ();
var B = new Sub ();
Console.log (b.arr.length); 0
a.arr[0]= ' moersing ';
Console.log (b.arr.length); 1

As can be seen, arr is a shared, if not applicable this.arr and with var arr = [], sub access is not available, only through the parent class's closed package access, but the problem still exists. If you want to inherit a fully independent reference type:
First:
function Base () {
This.arr = [];
}
function sub () {
Base.call (this);//Add an instance attribute Base.call (this) in this refers to the object that calls the base method, which is the Window object, and the call () method changes the image of the calling function.
}//For example, function A () {} The default call to a function is a window object, and function B () {this.name= "1"} In this refers to Window A.call (b) will mean B To invoke the object of a
Sub.prototype= new Base (); This sentence I understand as can be removed, there is no need .... Then there is no memory after the following delete--.
var a = new sub ();
var B = new Sub ();
Console.log (b.arr.length); 0
a.arr[0]= ' moersing ';
Console.log (b.arr.length); 0
Console.log (a.arr.length); Modified A/b not affected
This problem can be solved, but not perfect, as you can see, A and B have an instance property of Arr and a prototype arr, that is to say.
A.arr[0] = ' moersing ';
Console.log (a.arr.length); 1
Delete A.arr; Kill the attribute instance
Console.log (a.arr.length);//This is the time to visit prototype.
So, theoretically, this is not perfect, and variables require memory, right? Ok OK, then, next, use another one, the one above called borrowing inheritance.
The next way is perfect, but it's not so complicated, it's mainly using the idea of borrowing inheritance, that is to say, borrowing instances and borrowing prototypes separately.
function Base () {
This.arr = [];
}
Base. Constructor = function () {//Add a prototype constructor
Judging here, if it is a function and not a regular expression object, IE7 will reflect the regular as a function instead of an object.
if (typeof this = = ' function ' && typeof this.prototype.source = = = ' undefined ') {
var fn = This.prototype;
Fn.name = ' moersing ';
Fn.age = ' 18 ';
Fn.getfull = function () {
Console.log (this.name + ' | ' + this.age);
};
}
else {
throw new TypeError (' This was not a function ');
}
};
function sub () {
Base.call (this); To instantiate an instance property by borrowing a constructor
}
Base. Constructor.call (sub); Then call the parent class method to construct a prototype.
var a = new sub ();
var B = new Sub ();
A.arr[0] = ' moersing '; Add an element to the reference type of instance a
Console.log (a.arr.length); The result is 1.
Console.log (b.arr.length); The result is 0, which means that no impact has been received
Console.log (A.name); Print the properties of a A.prototype
Console.log (B.name); Print the properties of a B.prototype
B.name = ' Linfo '; Modify B's.
Console.log (B.name); Linfo
Console.log (A.name); A no effect, or a moersing
A.getfull (); Moerisng|18
B.getfull (); Linfo |18

Below is the constructor for the prototype design, where there is no instance of the Obejct object in the function as above, and the background will help you create the object, which represents the object and operates on it.

function Student (name,age) {this. Name = name; this. Age = Age ;  This function () {    alert ("prototype design constructor");}}

The following are the parasitic pattern constructors, which are generally not recommended and cannot be used for data sharing.

function Student (Name,class) {2   var obj=New  Object (); 3   obj.name = name  ; 4   Obj.class = class;   5 }varnew student ("Waypoint", "1 shifts");

JavaScript constructors. Prototype prototype understanding function. Call () understanding of method cloning

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.