Home >Web Front-end >JS Tutorial >Javascript creates custom objects, creates Object instances, adds properties and methods_javascript skills

Javascript creates custom objects, creates Object instances, adds properties and methods_javascript skills

WBOY
WBOYOriginal
2016-05-16 17:52:461281browse

As shown below:

Copy code The code is as follows:

var person = new Object();
person.name = "Nicholas";
person.age = "29"
person.job = "Software Engineer";

person.sayName = function () {
alert(this.name);
};

person.sayName(); The above example creates an object named person and adds three attributes to it (name, age and job) and a method (sayName()). Among them, the sayName() method is used to display the value of this.name(). Early JavaScript developers often used this pattern to create new objects. But this method has an obvious disadvantage: using the same interface to create many objects will produce a lot of duplicate code. To solve this problem, people started using a variant of the factory pattern.
Statement:
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