js的构造函数和原型
Posted xiaoxinstart
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了js的构造函数和原型相关的知识,希望对你有一定的参考价值。
1.构造函数创建对象
function Person(name,age,job) {
this.name = name;
this.age = age;
this.job = job;
this.sayName = function () {
alert(this.name);
}
}
var person1 = new Person("cherry", 25, "Engineer");
person1.sayName();
2.原型
function Person(name, age, job) {
this.name = name;
this.age = age;
this.job = job;
}
Person.prototype.sayName = function () {
alert(this.name);
}
var person1 = new Person("cherry", 25, "Engineer");
person1.sayName();
3.继承
this.name = name;
this.age = age;
this.job = job;
}
Person.prototype.sayName = function () {
alert(this.name);
}
function Chinese() {
Chinese.prototype = new Person("cherry", 25, "Egineer");//Chinese继承自Person
var chinese = new Chinese();
chinese.sayName();//chinese共享Person的function
以上是关于js的构造函数和原型的主要内容,如果未能解决你的问题,请参考以下文章