function Person(name,id){
//实例变量可以被继承
this.name = name;
//私有变量无法被继承
var id = id;
//私有函数无法被继承
function speak(){
alert("person1");
}
}
//静态变量,无法被继承
Person.age = 18;
//公有函数可以被继承
Person.prototype.say = function(){
alert("person2");
}
function Person(name,id){
//实例变量可以被继承
this.name = name;
//私有变量无法被继承
var id = id;
//私有函数无法被继承
function speak(){
alert("person1");
}
}
//静态变量,无法被继承
Person.age = 18;
//公有静态成员可被继承
Person.prototype.sex = "男";
//公有静态函数可以被继承
Person.prototype.say = function(){
alert("person2");
}
//创建子类
function Student(){
}
//继承person
Student.prototype = new Person("iwen",1);
//修改因继承导致的constructor变化
Student.prototype.constructor = Student;
var s = new Student();
alert(s.name);//iwen
alert(s instanceof Person);//true
s.say();//person2
function Person(name,id){
//实例变量可以被继承
this.name = name;
//私有变量无法被继承
var id = id;
//私有函数无法被继承
function speak(){
alert("person1");
}
}
//静态变量,无法被继承
Person.age = 18;
//公有静态成员可被继承
Person.prototype.sex = "男";
//公有静态函数可以被继承
Person.prototype.say = function(){
alert("person2");
}
//创建子类
function Student(){
}
//继承person
Student.prototype = new Person("iwen",1);
//修改因继承导致的constructor变化
Student.prototype.constructor = Student;
//保存父类的引用
var superPerson = Student.prototype.say;
//复写父类的方法
Student.prototype.say = function(){
//调用父类的方法
superPerson.call(this);
alert("Student");
}
//创建实例测试
var s = new Student();
alert(s instanceof Person);//true
s.say();//person2 student
function extend(Child, Parent) {
var F = function(){};
F.prototype = Parent.prototype;
Child.prototype = new F();
Child.prototype.constructor = Child;
Child.uber = Parent.prototype;
}
//创建父类
function Person(name,id){
//创建一个对象来承载父类所有公有东西
//也就是说_this承载的对象才会被传递给子类
var _this = {};
_this.name = name;
//这样的是不会传递下去的
this.id = id;
//承载方法
_this.say = function(){
alert("Person");
}
//返回_this对象
return _this;
}
//子类
function Student(){
//获取person的_this对象,从而模仿继承
var _this = Person("iwne",1);
//保存父类的_this引用
var superPerson = _this.say;
//复写父类的方法
_this.say = function(){
//执行父类的say
superPerson.call(_this);
alert("Student");
}
return _this;
}
var s = new Student();
s.say();
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有