javascript - 请教一个js的面试题目
PHP中文网
PHP中文网 2017-04-11 12:54:52
[JavaScript讨论组]

在中间的注释那里补充代码使这个代码输出最后的注释内容,谢谢大牛

var A = function() {
        this.name = 'apple';
    }
    A.prototype.getName = function() {
            return this.name;
        }
        // 补充代码

    var B = A.extend({
        initialize: function() {
            this.superclass.initialize.call(this);
            this.total = 3;
        },
        say: function() {
            return '我有' + this.total + '个' + this.getName()
        }
    });
    var b = new B();
    console.log(b.say()); //我有3个apple
PHP中文网
PHP中文网

认证高级PHP讲师

全部回复(2)
迷茫

Function.prototype.extend= Function.prototype.extend || function(obj) {

var self=this;
function SubClass(){
   this.superclass={initialize:self};
   if (obj.initialize)obj.initialize.call(this)
}

SubClass.prototype = new self();
SubClass.prototype.constructor = SubClass;

for(var key in obj){
  if(key !== 'initialize'){
    SubClass.prototype[key] = obj[key]

  }
}
return SubClass;

}
注:这块代码放在其他可行位置都是可以的哦~~~~
我替一位哥们儿写的(我只是个搬运工,逃~)

巴扎黑

试着回答了一下,太烧脑了。

var A = function() {
    this.name = 'apple';
}
A.prototype.getName = function() {
    return this.name;
}
/*
    答题开始
*/
A.initialize = function(){
    //没看明白考查点
};

A.extend = function( extend ){
    extend = extend || {};
    extend.superclass = this;
    if( "initialize" in extend ){
        extend.initialize();
    }
    for( pop in extend ){
        if( pop != "initialize" || pop != "superclass" ){
            this.prototype[pop] = extend[pop];
        }
    }
    return this;
}
/*
    答题结束
*/
var B = A.extend({
    initialize: function() {
        this.superclass.initialize.call(this);
        this.total = 3;
    },
    say: function() {
        return '我有' + this.total + '个' + this.getName()
    }
});
var b = new B();
console.log(b.say()); //我有3个apple
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送

Copyright 2014-2026 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号