var year = 2021
function getDate(month, day) {
console.log( this.year + '-' + month + '-' + day)
}
let obj = {year: 2022}
~function(){
function callSelf(context, ...args) {
if (typeof this !== 'function'){
throw new TypeError('error')
}
let key = Symbol('key'), result;
context[key] = this ;
result = context[key](...args)
delete context[key]
return result
}
Function.prototype.callSelf = callSelf;
}();
getDate.call(obj, 3, 15)
getDate.callSelf(obj, 6, 15)
Function.prototype.myapply=function(content) {
var res;
if(typeof this != "function"){
console.log("not a function");
}else{
content = content || window;
content.fn = this;
if(arguments[1]) {
res = content.fn(arguments[1]);
} else{
res = content.fn();
}
}
delete content.fn;
return res;
}
function Parent(work){
this.work = work;
}
function Child(name,work){
this.name = name;
Parent.myapply(this,work);
}
var child = new Child("张三","工人");
console.log(child,child.work,child.name);