Function.prototype.bind2 = function(context) {
const args = Array.prototype.slice.call(arguments, 1);
const self = this;
var fBind = function() {
return self.apply(this instance fBind ? this : context, args.concat(Array.prototype.slice.call(arguments)));
}
fBind.prototype = Object.create(self.prototype);
return fBind;
}
複製程式碼
function new2() {
var constructor = Array.prototype.shift(arguments); // 獲取建構函式,並且shift會改變arguments
var obj = new Object();
obj.__proto__ = constructor.prototype; // 加入原型鏈
var result = constructor.apply(obj, arguments);
return typeof result === 'object' ? result : obj; // 構造如果顯示return一個物件,則返回該物件,否則返回this;
}複製程式碼
function instanceof(x, y) {
while(x.__proto__!==null) {
if(x.__proto__===y.prototype) {
return true;
break;
}
x.__proto__ = x.__proto__.proto__;
}
if(x.__proto__==null) {return false;}
}複製程式碼
Object.defineProperty(Object, assgin2, {
wirte: true,
configurable: true,
enumerable: false,
value(target) {
if (target == null) throw new TypeError('Cannot convert undefined or null to object');
const results = typeOf target === 'Object' ? target : Object(target);
if (arguments.length > 1) {
for (let i = 1; i < arguments.length; i += 1) {
const arg = arguments[i];
if (arg !== null) {
const obj = typeOf arg === 'Object' || 'Function' ? arg : Object(arg);
for (const k in obj) {
if (Object.prototype.hasOwnProperty(obj, k)) {
results[k] = obj[k]
}
}
}
}
}
return results;
}
})複製程式碼