一般情况下,我们这样创建实例
function Student(name) {
this.name = name;
}
Student.prototype.hello = function () {
alert('Hello, ' + this.name + '!');
};
var xiaoming=new Student('小明');
console.log(xiaoming.name)
封装之后
function Student(name) {
this.name = name;
}
Student.prototype.hello = function () {
alert('Hello, ' + this.name + '!');
};
function createSrudent(name){
return new Student(name)
};
var xiaoming=createSrudent("小明")
console.log(xiaoming.name)