An in-depth understanding of JavaScript to implement object-oriented programming methods

Source: Internet
Author: User

Before introducing the idea of object-oriented programming in JavaScript, there are several concepts that need to be understood:

1. Shallow copy and deep copy: the variables that the program uses in the process of running are variables on the stack and variables on the heap, in the process of assigning objects or variables, most of the information is copied on the stack, so the following happens if the variable is an object, then this is the copy of the real object. The starting address of the heap memory space, which is known as a shallow copy, if it is a deep copy, it allocates a memory in the memory heap space and copies the starting address of the allocated memory.

2. Reference type data and value type data: Referring to reference type data and value type data, naturally reminiscent of memory heap and stack, reference type of data real content is stored on the heap of memory (the release of memory is through manual release or runtime garbage collection mechanism implemented), The data of the value type is stored on the stack of memory (after the function executes, the memory is freed by a pop-up stack). In fact, in most programming languages the assignment operation is the content on the copy memory stack, but this simple operation has a very different effect on the value type and the reference type, and for the data of the value type, the implementation class assigns the true value to the other variable, and for the reference type of data, This operation simply assigns the address information of the real value to the other variable, and the actual content information is stored on the heap space. "Value types in JavaScript are: Numeric values, Boolean types, null and undefined, etc., reference types are: arrays, objects and functions, etc."

3. Prototype chain of objects in JavaScript: All objects in JavaScript have a property of prototype, and this property itself is an object (again, it also has a prototype property), so that a layer of upward loop on the formation of a prototype chain, Knowing that the prototype chain reaches the level of object, and the prototype property of object is null, the prototype chain terminates. By using the prototype chain, you can add attributes to objects in JavaScript (not so much as objects, rather than classes), and the properties that are added in this way will be used by the instance of later new. If you add a property that is a value type, and the object is later instantiated, these properties will have their own copy version in each instantiated object, but if the attribute added through prototype is a reference type, all objects that are instantiated at a later time share the same version, so Manipulating this property through an object affects all instance objects at the same time. So, we should try not to use this method to add some properties of the "variable" type, but instead try to use this method to add some properties of the "method" type.

You can try to deepen your understanding of this concept by following the demo program:

varInnermodel=function(){     This. msge= "Init message";} Innermodel.prototype.msge=""; INNERMODEL.PROTOTYPE.CHANGEMSG=function(newmsge) { This. msge=Newmsge;};varInstance=NewInnermodel ();functionTestmodel (inparams) { This. arrtest=Inparams;} Testmodel.prototype.innermod=Instance;testmodel.prototype.show=function(){    //this.arrtest[0]=6;Alert This. Innermod.msge);} Testmodel.prototype.arrtest=[];varTestarr=NewTestmodel ([1,2,3,4]);varTestarr2=NewTestmodel ([4,5,6,7]); Testarr.show (); TESTARR.INNERMOD.CHANGEMSG ("New Mssage"); Testarr2.show ();
View Code

4. The constructor property of an object in JavaScript: The constructor property of an object in JavaScript is a property of the prototype object, and the constructor property is usually used to determine the type of an object. Often used in JavaScript object-oriented programming when inheriting.

5. The difference between object methods (class methods) and prototype methods in JavaScript: Object methods are called directly through object names, equivalent to static methods in other object-oriented programming languages, and the prototype method is invoked by instantiating a instance by each object (class). This means that each object instantiates a instance will have its own copy version (note that the copy here, just shallow copy, copy only the address of the method, and the real method body in memory only one copy).

6. The access process of the JavaScript Zhongyuan chain, when using an instantiated object to access a property or method, if the referenced method or property does not belong to the method or property of the object, the method or property is found to be the method or property of an object on its prototype chain, knowing that a matching Or arrive at the top of the prototype chain (that is, prototype is Null,object prototype is null), JavaScript is often used in programming to implement inheritance.

An in-depth understanding of JavaScript to implement object-oriented programming methods

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.