请教一个js的面试题目

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

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
阅读 2.8k
3 个回答

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

我的思路来自 ES6 Classes 的 ES5 实现
https://github.com/addyosmani...

// https://segmentfault.com/q/1010000008783768
var A = function () {
  this.name = 'apple'
}
A.prototype.getName = function () {
  return this.name
}

// 补充代码
function createSubClass(SuperClass, props) {
  // initialize 起着 constructor 的作用
  var initialize = props.initialize
  if (typeof initialize !== 'function') {
    initialize = function () {
      this.superclass.initialize.call(this)
    }
  }
  // SuperClass.prototype.initialize = SuperClass
  SuperClass.prototype.initialize = function () {
    SuperClass.call(this)
  }

  function SubClass() {
    this.superclass = SuperClass.prototype
    initialize.call(this)
    // 修复 superclass
    this.superclass = SuperClass.prototype
  }

  // SubClass.prototype
  var proto = Object.create(SuperClass.prototype, {
    constructor: {
      value: SubClass,
      enumerable: false, writable: true, configurable: true
    }
  })

  Object.getOwnPropertyNames(props).forEach(function (key) {
    if (key !== 'initialize') proto[key] = props[key]
  })

  SubClass.prototype = proto

  // 继承 static method
  Object.setPrototypeOf
    ? Object.setPrototypeOf(SubClass, SuperClass)
    : SubClass.__proto__ = SuperClass

  return SubClass
}

A.extend = function (props) {
  return createSubClass(this, props)
}

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

// 进一步测试
var C = B.extend({
  print: function () {
    console.log(this.total + '个' + this.getName())
  }
})
var c = new C()
c.print() // 3个apple
console.log(c instanceof B) // true
console.log(c instanceof A) // true

效果是达到了,不过有些别扭。

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
1 篇内容引用
推荐问题