function Person(name, age) { //构造函数里面的属性和方法 this._name = name; this._age = age; this.run = function(){ console.log(this._name, this._age); } // 静态属性和方法 // var hobby = 'basketball'; // var say = function(){ // console.log(this._name); // } } //原型链上的属性和方法 可以被多个实例共享 Person.prototype.sex='男'; Person.prototype.work = function(){ console.log(this._name, this._age); } //静态方法和属性 Person.hobby = 'basketball'; Person.say = function(){ console.log(this._name, this._age); } var p = new Person('张三', 20); // 实例方法通过实例化调用, 静态方法通过类名直接调用 p.run(); p.work(); Person.say(); // 继承 // 对象冒充实现继承 没法继承原型链上的属性和方法 function Man(name, age) { // Person.call(this, name, age); } // 原型链继承 继承原型链上的属性和方法 , 没法向父类传参 Man.prototype = new Person(); // 原型链继承不能把数据传到父类 var m = new Man('张三', 30); m.run(); // 一般两个结合使用 function Man(name, age) { Person.call(this, name, age); } Man.prototype = new Person();
评论 (0 )
最新评论
暂无评论
赶紧努力消灭 0 回复