Home > Web Front-end > JS Tutorial > body text

Summary of examples of several ways to create custom objects in Javascript

伊谢尔伦
Release: 2017-07-27 17:01:47
Original
1610 people have browsed it

Object constructor/object literal

Put aside the design pattern, use the most basic method, which is to first call the Object constructor to create an Object, and then add attributes to the object.

var student = new Object();
     student.name = "xiao ming";
     student.age = 20;
     student.getName = function () {
         alert(this.name);
     }
Copy after login

Students who are familiar with JavaScript object literals can change to a better way of writing, which at least looks more concise.

 var student = {
        name: "xiao hong",
        age: 18,
        getName: function () {
            alert(this.name);
        }
    };
Copy after login

Disadvantages: A disadvantage of the above method is that when using the same interface to create many similar objects, a lot of duplicate code will be generated. This should be easy to understand. Functions (methods or classes) are generally used to create public methods. The above object creation process has no shadow of functions at all, so there is no reuse.

Constructor of custom type:

Constructor can be used to create objects of specific types.

  function Student(name,age) {
         this.name = name;
         this.age = age;
         this.sayName = function () {
             alert(this.name);
         }
     }
     var p3 = new Student("ming", 20);
     var p4 = new Student("hong", 18);
     alert(p3 instanceof Student); 
    alert(p3.sayName==p4.sayName); //false
Copy after login

Disadvantages: The disadvantage of custom constructors is that each object will recreate its own method. In fact, the functions of these methods are the same (like sayName), but they are not the same (p3.sayName and p4.sayName is not equal).

The combination of constructor and prototype:

     function Student(name, age, friends) {
         this.name = name;
         this.age = age;
         this.friends = friends;
     }
     Student.prototype = {
         constructor: Student,
         sayName: function () {
             alert(this.name);
         }
     };
Copy after login

Summary: The combination of constructor and prototype is a widely recognized method for creating custom types. It is also the best method among the above methods.

The above is the detailed content of Summary of examples of several ways to create custom objects in Javascript. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template