// 1.通过原型为一个已经编写好的类型增加一个扩展方法
//比如给string增加一个sayhello 或者addwjx方法
var s = '今天天气好晴朗,处处好风光';
String.prototype.sayHello = function () {
alert('我是字符串');
};
s.sayHello();
String.prototype.addStar = function () {
alert('☆' + this + '☆');
};
s.addStar();
//2.通过原型继承对象
function Person(name, age, gender) {
this.name = name;
this.age = age;
this.gender = gender;
}
Person.prototype.sayHello = function () {
alert('我是Person对象的SayHello,' + this.name + this.age + this.gender);
};
//2.1继承
function Student(id,classname) {
this.stId = id;
this.classname = classname;
}
var p = new Person('', 0, '');
Student.prototype = p;
var s = new Student(002, '远洋班')
s.name = '王宝宝';
s.age = 19;
s.gender = '女';
s.sayHello();
//2.2重写
s.sayHello = function () {
alert('这是student类的sayhello' + this.name + this.age + this.gender)
};
s.sayHello();
//2.3继承关系:对象继承父类,父类继承父类的父类,最终继承与object
js-面向对象=继承-多态
最新推荐文章于 2023-06-19 17:23:52 发布