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

javascript最常用与实用的创建类的代码_js面向对象

WBOY
Release: 2016-05-16 18:21:28
Original
976 people have browsed it
复制代码 代码如下:

//以构造函数方式添加私有属性和方法
function Person(name, age, address) {
this.name = name;
this.age = age;
this.address = address;
}
//以原型方式添加公有属性、方法
Person.prototype = {
constructor: Person,
showName: function () {
alert(this.name + this.age + this.address);
}
}
//使用
var person = new Person("zhangsan", 22, "中国北京!");
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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!