search

Home  >  Q&A  >  body text

javascript - 如下代码,什么情况下typeof Constructor.prototype === 'number'?

function A( name ){
    this.name = name;
}
 
function ObjectFactory(){
    //console.log(arguments);
    var obj = {},
    Constructor = Array.prototype.shift.call( arguments );
    //console.log(typeof Constructor.prototype);
    
    obj.__proto__ =  typeof Constructor.prototype === 'number'  ? Object.prototype : Constructor.prototype;
    
    var ret = Constructor.apply( obj, arguments );
    //console.log(typeof ret);
    
    return typeof ret === 'object' ? ret : obj;
    
}
 
var a = ObjectFactory( A, 'svenzeng' );
巴扎黑巴扎黑2828 days ago460

reply all(2)I'll reply

  • PHP中文网

    PHP中文网2017-04-11 11:54:55

    你在 var a = ObjectFactory( A, 'svenzeng' );
    前面加一句 A.prototype = 10; 就是了。

    reply
    0
  • 天蓬老师

    天蓬老师2017-04-11 11:54:55

    你这个永远都不会等于number,因为手机码字,所以点到为止,number是基本数据类型,而prototype是引用数据类型object

    reply
    0
  • Cancelreply