JavaScript's prototype (prototype)

zhaozj2021-02-08  408

Written in front: This is some of my time and Jeff.yan discussion, mainly about JavaScript's Design Pattern, because there is no finishing, is the most original email content, I will post a little about it, I Some of his answered and discussion, I will put as complete as possible with your own opinion.

Because it is a discussion of Jeff.yan, the publication of the content of my message is simultaneously signed, and the reply to me is given to me, if he consents, I will still specifically explain it.

JavaScript Prototype implementation

Author: Jeff.Yan (Yan Hong), BlueSwing.Liu (Liuru Hong)

mode:

Prototype (original model mode or prototype mode)

definition:

By giving a prototype object to specify the type of object to be created, then create more types of objects with this prototype object, the original model mode belongs to the object of the object.

JavaScript implementation:

In the Java language, the objects are inherited from java.lang.object, while java.lang.Object provides Clone-by-way ways, as long as the interface cloneable, that is, supporting Clone, otherwise the exception is thrown. In this JavaScript is very close, all objects are inherited from Object, but Object does not support Clone methods, but we can implement Clone methods through its own form of JavaScript through ExpandDo, so that all objects are created in the future. Clone method.

Because JavaScript itself does not provide Clone method, the assignment of the object such as var A = new object (); var b = a, such code A, B is pointing to the same object, to create an object must pass the key to this keyword To achieve, therefore in the implementation of Clone, I define a constructor Clonemodel, and specify its parent object to the object of the Clone activity itself, so use this keyword, in our defined constructor Clonemodel On the basis of we create an object, because there is no code inside the constructive, the newly created object actually said that all implementations are in the parent object, that is, we need to make Clone objects. So far, we have created a object that needs to be copied, but all the values ​​point to the parent object.

In the object-oriented mode of JavaScript, we have discussed that if there is no value of the parent object, then this time is directly to the parent object, and the internal value of the object after the prototype pattern is required to be the internal value of the object after clone. Just assign a value once, the value of ObjClone will be in their own memory space, not to the parent object. Based on this consideration, objclone [v] = objclone [v]; statement is to implement the value of the parent object to copy to its own memory by overwritten. (The memory mentioned here should be in the sense of logic)

Realization

After completing the above work, just a shallow copy, the object is still a reference to the object. This time you can get the attribute object of the CloneD object by calling the CLONE method of the object (because I don't know how to say it). ObjClone [v] = objclone [v] .clone (); this code is to complete this function.

CLONE method

//

/ / Method for adding clone for Object, because all objects of the object are Object

/ / So all user-defined objects implemented Clone methods //

Object.prototype.clone = function () {

Function clonemodel () {

}

Clonemodel.Prototype = this;

Var objclone = new clonemodel ();

Var strmsg = "";

For (vin objclone) {

Switch (TypeOf ObjClone [V]) {

Case "function":

// If it is a method, you don't need to perform Clone

Break;

Case "Object":

///

// If it is an object, use Clone to re-get, the purpose of doing the depth Clone

// Because JavaScript is a language of an Object Based, otherwise the internal object is a reference to the original reference.

///

Objclone [v] = objclone [v] .clone ();

Break;

DEFAULT:

///

// All remaining data types All re-value

// The purpose of this is to ensure that the storage in memory is in the space of the new object.

/ / Not only point to a refrence of Parent Object

///

ObjClone [v] = objclone [v];

}

}

Return Objclone;

}

Object class definition

Function bookInfo (vcaption) {

THIS.CAPTION = VcAption;

VAR CURPAGE = 0;

This.SetPage = Function (vdata) {

Curpage = VDATA;

}

THIS.GETPAGE = Function () {

Return Curpage;

}

}

Test code

//

// Test BookInfo 'S Clone Method

//

//

Function test () {

Var objtest = New bookInfo ("JavaScript prototype pattern";

Objtest.SetPage (1000);

Objtest.author = "ruhong.liu"; // Object expanddo

ShowObject (Objtest, "Original Object");

// Clone Object from Objtest

Var objcloned = Objtest.clone ();

Showobject (ObjCloned, "Object after Clone");

// if you change the objte's CAPTION

// You can Find Objcloned's CAPTION HAS BE CHANGED

Objtest.caption = "Changed Base Object";

// show message

ShowObject (Objtest, "Modify the original object after CAPTION);

Showobject (ObjCloned, "Modify CLONE Object after CAPTION);

/ *

// ---------- This code can not work ---------------------- //

// Now you can change ObjCloned's CAPTION

ObjCloned.caption = "Hello, Jeff.yan";

// show message

ShowObject (Objtest, "CLONE object CAPTION) After the original object");

ShowObject (ObjCloned, "Clone object CAPTION changes"); * /

}

Function Showobject (o, vcAption) {

Var strmsg = vcaption "/ n";

STRMSG = "CurrentPage:" O.getPage () "/ n";

strmsg = "CAPTION:" O.caption "/ n";

STRMSG = "Expanddo Property Author:" O.author;

Alert (STRMSG);

}

Conclusion:

According to my current understanding and testing, I think the prototype keyword is not the implementation of the prototype mode, which can be verified by Parent Object.

转载请注明原文地址:https://www.9cbs.com/read-329.html

New Post(0)