本文介绍了对象原型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

function Father(name,age) {
    this.name = name;
    this.age = age;
    if (typeof Father._initialized == "undefined") {
        Father.prototype.showname = function () { document.writeln("My name is " + this.name); };
        Father.prototype.sex="male";
        Father._initialized = true;
    }
}
function Son(name, age, girlfriend) {
    Father.apply(this, arguments);
    this.girlfriend = girlfriend;
    Son.prototype = new Father();
}
var son = new Son("shiy", 7, false);
son.showname();   //will make mistake
var x=new Son("shilf",23,false)
x.showname();      //it is right



为什么son.showname()会出错?



why son.showname() will make mistake?

推荐答案

var son = new Son("shiy", 7, false);
son.showname();   //will make mistake
var x=new Son("shilf",23,false)
x.showname();


这篇关于对象原型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-23 08:14