Object.create = Object.create || function(prototype) {
var F = function () {};
F.prototype = prototype;
return new F();
}
function _new () {
var args = [].slice.call(arguments);
var constructor = args.shift();
var context = Object.create(constructor.prototype);
var result = constructor.apply(context,args);
return typeof result === 'object' && result !=null ? result : context;
}
3.如何防止因没有使用new命令,而发生意外
var Tu = function(obj) {
if ( this instanceof obj) { return obj }
if (!(this instanceof obj) ) { return new Tu(obj);}
// 构造函数执行一些动作
};
Es5
一. new命令背后发生了什么
1. 原理:
2. 相关代码
3.如何防止因没有使用new命令,而发生意外