Some ways that JavaScript creates objects

Source: Internet
Author: User
Tags uppercase letter

By creating an object instance
var person = new Object (); Person.name = "Zhouquan"; Person.age = 21; Person.sayname = function () {   console.log (this.name);};  Person.sayname ();//zhouquan
Object Literal method
var person = {  Name: "Zhouquan",  age:21,  sayname:function () {    console.log (this.name);  }}; Person.sayname ();//zhouquan

The disadvantage of creating objects in a way that creates an object instance and object literals is that neither of these methods is suitable for creating objects of the same type more than once.

Factory mode
function Createperson (name, age, Job) {   var o = new Object ();   O.name = name;   O.age = age;   O.job = job;   O.sayname = function () {    console.log (this.name);   };   return o; }  var = createperson ("Zhouquan", "Person1", "student"); Person1.sayname ()  ; Zhouquan      var person2 = Createperson ("zhouquan2", "Programer"); Person2.sayname ();  Zhouquan2

The function Createperson () is able to construct a person object that contains all the necessary information based on the parameters that are accepted. This function can be called countless times, and each time it returns an object that contains a number of attributes and a method. Although the factory model solves the problem of creating multiple similar objects, it does not solve the problem of object recognition (that is, how to know the type of an object).

Constructor mode
function person (name, age) {  this.name = name;  This.age = age;  This.sayname = function () {    console.log (this.name);  };} var person1 = new Person ("Zhouquan", +);p erson1.sayname ();  Zhouquanvar Person2 = new Person ("zhouquan2");p erson2.sayname ();  Zhouquan2

Creating an object in this way is more of a factory mode than it does with the following differences:

    • Create objects that are not displayed
    • Assign properties and methods directly to the This object
    • No return statement

In addition, you should also be aware of the uppercase letter P used by the function name person. This is to distinguish it from other functions.

Prototype mode
function person () {}person.prototype.name = "Zhouquan"; Person.prototype.age = 21; Person.prototype.sayName = function () {  console.log (this.name);}; var person1 = new Person ();p erson1.sayname (),//zhouquanvar person2 = new Person ();p erson2.sayname ();//zhouquan

prototype (prototype) property, which is a pointer to an object that is used to contain properties and methods that can be shared by all instances of a particular type. The advantage of using a prototype object is that you can have all of the object instances share the properties and methods that it contains.

But this method is still inconvenient to write, need to write person.prototype in front of each property or method, I can combine the previous object literal method to improve:

function person () {}person.prototype={  name: "Zhouquan",  age:21,  sayname:function () {    Console.log ( this.name);  }}; var person1 = new Person ();p erson1.sayname (),//zhouquanvar person2 = new Person ();p erson2.sayname ();//zhouquan
Combining the constructor pattern with the prototype pattern
function person (name, age) {  this.name = name;  This.age = age;} person.prototype={  sayname:function () {    console.log (this.name);  }}; var person1 = new Person ("Zhouquan", +);p erson1.sayname ();//zhouquanvar person2 = new Person ("Xiaozhou", 21); Person2.sayname ();//xiaozhou

This approach is the most common way to create custom types. The constructor pattern is used to define instance properties, and the prototype schema is used to define methods and shared properties. In this way, each instance will have its own copy of the instance properties, but at the same time share a reference to the method to maximize memory savings. In addition, this combination pattern also supports passing parameters to the constructor. It can be said that this is one of the default patterns used to define reference types.

Dynamic Prototyping Mode
function person (name, age) {  this.name = name;  This.age = age;  if (typeof this.sayname! = "function") {    Person.prototype.sayName = function () {      console.log (this.name);    };  }}var Person1 = new Person ("Zhouquan", +);p erson1.sayname ();//zhouquanvar person2 = new Person ("Xiaozhou", 21); Person2.sayname ();//xiaozhou

This is only added to the prototype if the Sayname () method does not exist, and the IF statement executes only when the function is first called. Where the If statement checks for any properties or methods that should exist after initialization, you do not have to check each property and method with a large heap of if statements, just check one. It is important to note that when using the dynamic prototype pattern, you cannot rewrite the prototype with object literals, because if you rewrite the prototype with the instance created, the connection between the existing instance and the new prototype is severed.

Some ways that JavaScript creates objects

Related Article

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.