Several methods for creating objects in javascript
Source: Internet
Author: User
Javascript allows you to create a single Object by using Object constructor or Object literal. When we want to create many objects, simply using these two methods will produce a large number of duplicates. Here, I have summarized several object creation modes. This article is reading "Objective C... syntaxHighlighter. all (); javascript allows you to create a single Object by using Object constructor or Object literal. When we want to create many objects, simply using these two methods will produce a large number of duplicates. Here, I have summarized several object creation modes. This article is summarized after I read javascript advanced programming. 1. Factory mode this mode uses functions to reduce code duplication, and uses function parameters as interfaces to interconnect with object attributes and methods. Copy the code function createfactory (name, age) {var obj = new Object (); obj. name = name; obj. age = age; return obj;} var test1 = createfactory ('aaa', 18); var test2 = createfactory ('bbb ', 50 ); copying the code completes simple code reuse and achieves code duplication, but the type of the created object cannot be determined. In the constructor mode, we all know that Object, Array, and other native constructor can be used to create objects. The constructor mode is similar to this mode. You can create a constructor to create a new type of object. Copy the code function Createfunction (name, age,) {this. name = name; this. age = gae; this. sayname = function () {alert (this. name) ;}} var test1 = new Createfunction ('aaa', 20); var test2 = new Createfunction ('bbb ', 50); copy the code according to the Convention, the constructor should start with an uppercase letter. This mode can determine the type of the new object. Then this mode still has a problem. Both test1 and test2 instances have the sayname method. In javascript, the function is an object. Therefore, when test1 and test2 are created, two function objects are instantiated, similar to this: Copy code ### in prototype mode, each function has a prototype attribute pointing to its prototype object. A prototype object allows all object instances to share its attributes and methods. Let's look at the example below. '''Javascriptfunction Cf () {} Cf. prototype. name = "aaa"; Cf. prototype. age = 29; Cf. prototype. sayname = function () {alert (this. name);} var test1 = new Createfunction (); var test2 = new Createfunction (); copy the code above this method by adding the attributes and methods to the prototype object of the constructor, in this way, all instances of the new object will share these attributes and methods. To further reduce the amount of code, someone proposed a method to rewrite the prototype object. Let's look at the example below. Copy the code function Cf () {} Cf. prototype = {name = "aaa"; age = 29; sayname = function () {alert (this. name) ;}}var test1 = new Createfunction (); var test2 = new Createfunction (); copy the Code. This method further reduces code duplication by overwriting the prototype object, however, such writing poses new problems. When an object is created in the Prototype mode, the dynamic changes of the Prototype object are reflected in the instance in real time, because the [[Prototype] attribute in the instance directs to the Prototype object, in this way, the attributes and methods in the prototype object will be searched for in the upward search process of calling properties and methods. When an object is instantiated first, and then the Prototype object is rewritten, [[Prototype] In the instance no longer points to the new Prototype object, as a result, a new prototype object cannot be searched up during the call process. When you use the method to override the prototype object, you must first override the object and then create an instance. The prototype object mode still has disadvantages. Although it does not include parameter passing for the function, the instance obtains the same attribute value by default. In addition, for attributes that contain reference types, modifying instance attributes will cause the properties of the prototype object to change synchronously. In this way, the changes are reflected in all instances in real time. Obviously this is not the result we want. In connection with the previously mentioned constructor mode, we can combine the constructor mode and the prototype mode to solve this problem. Copy the code function Person (name, age, job) {this. name = name; this. age = age; this. job = job; this. friends = ["Shelby", "Court"];} Person. prototype = {constructor: Person, sayName: function () {alert (this. name) ;}}; var person1 = new Person ("Nicolas", 29, "Software Engineer"); var person2 = new Person ("Greg", 27, "Doctor"); copy the Code to define instance attributes in the constructor, and define methods and shared attributes in the prototype mode. In this way, each instance has its own copy of instance attributes, and also has shared attributes and methods, saving memory. You can also define instance attributes by passing parameters. The dynamic prototype mode is equivalent to a variant of the prototype mode. It encapsulates all the information in the constructor and initializes the prototype object only when necessary. Copy the code function Person (name, age, job) {// properties this. name = name; this. age = age; this. job = job; // methods if (typeof this. sayName! = "Function") {Person. prototype. sayName = function () {alert (this. name) ;}}}var friend = new Person ("Nicolas", 29, "Software Engineer"); friend. sayName (); In this constructor, the copy Code determines whether to initialize the prototype object by determining whether the property exists. In this way, the prototype object will be initialized only when the object is instantiated for the first time, which is perfect. Parasitic constructor mode when we want to create a constructor with an extra method for an existing constructor, we can use this mode because the existing constructor cannot be rewritten. Let's look at the example. Copy the code function SpecialArray () {// create the array var values = new Array (); // add the values. push. apply (values, arguments); // assign the method values. toPipedString = function () {return this. join ("|") ;}; // return it return values;} var colors = new SpecialArray ("red", "blue", "green "); in the example of copying code, an array with additional methods is created with additional special methods. A new constructor is created without modifying the array constructor. However, the objects returned by the parasitic constructor mode have no relationship with the prototype object of the constructor.
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