在js中可以用对象冒充来实现继承机制,我做了个例子如下所示:function people(name,age){
this.name=name;
this.age=age;
this.sayHello=function(){
alert("Hello,"+this.name)
}
}
function student(name,age,grade,school){
this.people=people;
this.people(name,age)
this.grade=grade;
this.school=school;
}
var s=new student("小伟",25,"classI","中国大学");
s.sayHello()这样可以弹出"Hello,小伟",我修改了代码,如下所示:function people(name,age){
this.name=name;
this.age=age;
this.sayHello=function(){
alert("Hello,"+this.name)
}
}
function student(name,age,grade,school){
//this.people=people;
//this.people(name,age)
this.people=function(name,age){
this.name=name;
this.age=age;
this.sayHello=function(){
alert("Hello,"+this.name)
}
}
this.grade=grade;
this.school=school;
}
var s=new student("小伟",25,"classI","中国大学");
//s.people("然思维",25)
s.sayHello()这时再执行就不弹出了,直接报错,说s.sayHello()不是一个function,我想问下这是为什么?区别在哪里?
this.name=name;
this.age=age;
this.sayHello=function(){
alert("Hello,"+this.name)
}
}
function student(name,age,grade,school){
this.people=people;
this.people(name,age)
this.grade=grade;
this.school=school;
}
var s=new student("小伟",25,"classI","中国大学");
s.sayHello()这样可以弹出"Hello,小伟",我修改了代码,如下所示:function people(name,age){
this.name=name;
this.age=age;
this.sayHello=function(){
alert("Hello,"+this.name)
}
}
function student(name,age,grade,school){
//this.people=people;
//this.people(name,age)
this.people=function(name,age){
this.name=name;
this.age=age;
this.sayHello=function(){
alert("Hello,"+this.name)
}
}
this.grade=grade;
this.school=school;
}
var s=new student("小伟",25,"classI","中国大学");
//s.people("然思维",25)
s.sayHello()这时再执行就不弹出了,直接报错,说s.sayHello()不是一个function,我想问下这是为什么?区别在哪里?
s.people.sayhello();这样试下
function people(name, age) {
this.name = name;
this.age = age;
}
people.prototype.sayHello = function() {
alert("Hello," + this.name)
}
function student(name, age, grade, school) {
this.name = name;
this.age = age;
this.grade = grade;
this.school = school;
}student.prototype = new people();
student.prototype.getSchool = function() {
alert(this.school);
};
var s = new student("小伟", 25, "classI", "中国大学");
s.sayHello();
s.getSchool();
this.name=name;
this.age=age;
this.sayHello=function(){
alert("Hello,"+this.name)
}
}
function student(name,age,grade,school){
// 利用函数的call()或者apply()方法也可以实现继承
people.call(this,name,age);
this.grade=grade;
this.school=school;
}
var s=new student("小伟",25,"classI","中国大学");
s.sayHello()
this.people=people;
this.people(name,age)
这样调用时,people定义里的this.sayHello里的this是指student对象this.people=function(name,age){
this.name=name;
this.age=age;
this.sayHello=function(){
alert("Hello,"+this.name)
}
}
这样调用时,this.sayHello里的this指的是this.people,所以应该是s.people.sayHello()感觉挺绕的……
又想了一下,我在楼上说的不正确
实际上第二种方法不能用是在因为对象声明this.people只是赋了值,但是未被执行过,即是说在调用student.people之前,this.sayHello=function()...实际未执行
所以应该先执行s.people方法,再调用sayHello函数,即
s.people(s.name, s.age);
s.sayHello();
//this.people=people;
//this.people(name,age)
this.people=function(name,age){
this.name=name;
this.age=age;
this.sayHello=function(){
alert("Hello,"+this.name)
}
}
this.people();
this.grade=grade;
this.school=school;
}
上面那段FUNCTION还没有没执行 只是被赋予了people
后面加句 this.people();应该就可以了
你这样写当然不能这样调用了,应该是s.people.sayHello();
当然你样就不是继承了,相当于重写
<script type="text/javascript">function student(name,age,grade,school){
//this.people=people;
//this.people(name,age)
this.people=function(name,age){
this.name=name;
this.age=age;
this.sayHello=function(){
alert("Hello,"+this.name)
}
}
this.grade=grade;
this.school=school;
}
var s=new student("小伟",25,"classI","中国大学");
s.people("然思维",25);
s.sayHello();</script>
<script type="text/javascript">
function student(name,age,grade,school){
this.people=function(name,age){
this.name=name;
this.age=age;
this.sayHello=function(){
alert("Hello,"+this.name)
}
}
this.people(name,age);
this.grade=grade;
this.school=school;
}
var s=new student("小伟",25,"classI","中国大学");
s.sayHello();</script>
this.name=name;
this.age=age;
this.sayHello=function(){
alert("Hello,"+this.name)
}
}
function student(name,age,grade,school){
this.people=people;
this.people(name,age)
this.grade=grade;
this.school=school;
}
var s=new student("小伟",25,"classI","中国大学");
s.sayHello()this.pepole只是赋值但是没有执行,对象冒充其实和用call来继承一样,上面的写法等价于:function people(name,age){
this.name=name;
this.age=age;
this.sayHello=function(){
alert("Hello,"+this.name)
}
}
function student(name,age,grade,school){
people.call(this,name,age);
this.grade=grade;
this.school=school;
}
var s=new student("小伟",25,"classI","中国大学");
s.sayHello()
而javascript是动态执行的。所以写在闭包内的this并不认识外面的function
必须要执行下。才能把里面的this,动态链接到外面的this上。从而实现继承。
p.s.
我认为你的第一种对象冒充写法还是不完全
function people(name,age){
this.name=name;
this.age=age;
this.sayHello=function(){
alert("Hello,"+this.name)
}
}
function student(name,age,grade,school){
this.people=people;
this.people(name,age);
delete this.people;
this.grade=grade;
this.school=school;
}
var s=new student("小伟",25,"classI","中国大学");
s.sayHello();
这句话很重要,否则你的子类中会多一个this.people属性