[Repost] JavaScript Prototype implementation

zhaozj2021-02-16  56

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 have enabled Clone method // Object.Prototype.clone = function () {Function Clonemodel ) {} Clonemodel.prototype = this; var objclone = new clonemodel (); var strmsg = ""; for (vin objclone) {switch (type "function": // If it is a method, Do not need ClONE BREAK; CASE "Object": // // If it is an object, use Clone to re-get, the purpose is to perform depth clone // because JavaScript is a language of an Object based, otherwise the internal object is pointing The original reference /// ObjClone [v] = objclone [v] .clone (); break; default: // // The remaining data type is all re-assaped // This is to ensure that the value is in memory. Storage is in the space of the new object / / not just a refrence /// ObjClone [V] = ObjClone [V];}} Return Objclone;}}}}}}}}}}}}}}}}}}}}}}}}}}

Function bookinfo (vcAption) {this.caption = vcaption; var curpage = 0; this.SetPage = function (vdata) {curpage = function ()} this.getpage = function () {return.}

Test code

/// // Test bookinfo 's clone method // // function test () {var objte = 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, "after the object Clone"); // if you changed the objTest's caption // you can find objCloned's caption has be changed objTest.Caption = "changed Base Object"; // show message ShowObject (objTest, "the original object after revision Caption"); ShowObject (objCloned, "clone objects after revision Caption"); / * // ---------- This code can not work ---------------------- // // Now you CAN change objcloned's capen Objcloned.caption = "Hello, jeff.yan"; // show message showobject (original object after CAPTION); SHOWOBJECT (ObjCloned, "Clone Object CAPTION After modification"); * /} Function Showobject (o, vcAption) {var strmsg = vcaption "/ n"; strmsg = "CURRENTPAGE:" O.GETPAGE () "/ n"; strmsg = "CAPTION:" o.caption " / N "; strmsg =" expanddo protyor: " 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-19600.html

New Post(0)