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
Release: 2016-05-16 17:52:46
Original
1292 people have browsed it

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.
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