Creation of JavaScript objects

Source: Internet
Author: User
Tags access properties

The object-oriented language has a very significant flag, that is, they all have the concept of class, through the inheritance between classes can be achieved any object with the same property method. There is no concept of class in ECMAScript, which defines an object as a collection of unordered attributes that contain basic values, objects, or functions. This also means that each property or method of an object has a name, each name corresponds to a value, and we can access the corresponding value through these names. So how do we create these objects? The following is a detailed description of the method of object creation in seven.

1. Factory mode

Factory mode is a well-known design pattern in the field of software engineering, which abstracts the process of creating concrete objects. Given the notion that there is no class in ECMAScript, the developer takes the creation of a function and then uses this function to create the details of the object in a particular interface. Examples are as follows:

function Createperson (name,age,job) {     var p=New  Object ();     P.name=name;     P.age= Age;     P.job=job;     P.sayage=function() {          alert (this. Age);      };      return p;}
var person=Createperson ("SYF", "All", "Stu");

2. Constructor mode

There are primitive constructors in ECMAScript, such as Object,arrary, which we can use to create objects. In addition, we can also create custom constructors that define properties and methods for custom object types. Examples are as follows:

 function   person (name,age,job) { Span style= "color: #0000ff;"     >this . Name=name;      this . Age=age;      this . Job=job;  function   () {alert ( this  .age); };} var  person=new   person ("SYF", "All", "Stu"); 

By comparing the methods of factory and constructor, it can be seen that the code inside their functions is very similar. However, in the constructor mode we do not display the creation object (new object), but instead directly assign the property and method to the This object, and do not use the return statement to return the result. To create an object from a constructor pattern, you need to use the new operator.

The only difference between constructors and other functions is that they are called in a different way, and any function that is called by the new operator can be used as a constructor and vice versa as a normal function.

3. Prototype mode

Each function has a prototype property, which is a pointer to an object that contains properties and methods that can be shared by specific types of instances. Literally, prototype is the prototype object of the object instance created by invoking the constructor, with the properties and methods shared by the object instance. In other words, instead of defining the information for an object instance in the constructor, you can add that information directly to the prototype object. Examples are as follows:

function Person () {}person.prototype.name= "SYF"; Person.prototype.age= "All"; Person.prototype.job= "Job"; Person.prototype.sayAge=function() {    alert (this. age);}; var New Person ();

Objects created by the prototype schema are accessed by the same set of properties and methods, and all instances get the same property values by default. Object instances can access properties and methods in the prototype, but cannot override values in the prototype. If you add a property in the instance with the same name as the prototype, it will be created in the instance, and the new property will also mask the properties and methods in the prototype. This property or method, even if set to NULL, will no longer restore links to the prototype unless we completely remove the instance properties using the delete operator to restore access to the properties in the prototype. It can also be understood that by creating an object instance from the prototype schema, the instance is a mapping of the prototype, the modified instance is not reflected on the prototype, and the changes on the prototype affect the instance.

4. Combining the constructor pattern with the prototype mode

The combination is the most common way to create custom types in many ways. This is because in combinatorial mode, we use constructors to define instance properties, to define methods and shared properties in a prototype pattern, and eventually each instance has its own copy of instance properties, as well as a shared approach, which saves memory significantly. Examples are as follows:

function Person (name,age,job) {     this.name=name;     This.age= age;     this.job=Job;     this.friends=["zz", "FZW"]; }
person.prototype={
Constructor:person;
Sayage:function () {alert (this.age);
}
}
var person=New person ("SYF", "All", "Stu");

5. Dynamic Prototyping Mode

It is essentially a prototype that is initialized by a constructor, when needed, or when necessary. You can also understand that you can decide whether to initialize a prototype by checking that a method that should exist is valid. It encapsulates all of the information in the constructor, and also guarantees the advantage of creating objects using the combined pattern. The example is as follows:function person (name,age,job) {

function Person (name,age,job) {
this. name=name;      this. age= age;      this. job=job;     if (typeof this.sayage! = "function") {
person.prototype={
Sayage:function () {alert (this.age);}
}
}
var person=New person ("SYF", "All", "Stu");

6. Parasitic constructor mode

The basic idea is to create a function that encapsulates the code that creates the object, and then returns the object that was created. In addition to using the new operator to create objects, this method is no different from the factory schema.

function Person (name,age,job) {     var p=New  Object ();     P.name=name;     P.age= Age;     P.job=job;     P.sayage=function() {          alert (this. Age);      };      return p;} var New Person ("SYF", "All", "Stu");

When an object is created with a parasitic constructor, its returned object is not related to the constructor or to the stereotype property of the constructor. The object returned by the constructor is not different from the object created outside the constructor, and you cannot use the instanceof operator to determine the object type.

Creation of JavaScript 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.