Home > Web Front-end > JS Tutorial > The most commonly used and practical code for creating classes in javascript_js object-oriented

The most commonly used and practical code for creating classes in javascript_js object-oriented

WBOY
Release: 2016-05-16 18:21:28
Original
1056 people have browsed it
Copy code The code is as follows:

//Add private properties and methods in constructor mode
function Person(name, age, address) {
this.name = name;
this.age = age;
this.address = address;
}
//Add public in prototype way Properties, methods
Person.prototype = {
constructor: Person,
showName: function () {
alert(this.name this.age this.address);
}
}
//Use
var person = new Person("zhangsan", 22, "Beijing, China!");
person.showName();
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