对象原型 [英] Object.prototype

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

问题描述

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?

推荐答案

奇怪的是两个syntex都是相同的...您确定没有使用任何syntex错误.但根据您的代码,它的外观正确.
its strange both syntex are same... are you sured that you are not using any syntex error. but according to your code its look right.
var son = new Son("shiy", 7, false);
son.showname();   //will make mistake
var x=new Son("shilf",23,false)
x.showname();


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

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆