js基础函数

call(thisArg, arg1, aeg2, …)

把需要改变this指向的方法挂载到目标this上执行并返回
调用一个对象的方法,以另一个对象替换当前对象,如果没有就用Global对象替换
参数:

  • thisArg 当要被用作当前对象的对象
  • arg1, aeg2, … 要传入方法的参数
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    Function.prototype.mycall = function(context=window){
    if ( typeof this != 'function' ){
    throw new TypeError('not function')
    }
    context.fn = this
    var args = [...arguments].slice(1)
    var results = context.fn(...args)
    delete context
    return results
    }
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
//demo1
function add(a,b) {
alert(a+b);
}
function sub(a,b) {
alert(a-b);
}
add.call(sub,3,1);
//demo2
function c1(){
this.name = 'c1';
this.consoleLog = function(){
console.log(this.name)
}
}
function c2(){
this.name = 'c2';
this.consoleLog = function(){
console.log(this.name)
}
}
var class1 = new c1()
var class2 = new c2()
class1.consoleLog.call(class2)

apply(thisArg, […])

调用一个对象的方法,以一个对象替换当前对象,(同call,只是参数不一样)

1
2
3
4
5
6
7
8
9
10
11
Function.prototype.myapply = function(context){
if(typeof this !== 'function'){
throw new TypeError('this not function')
}
context = context || window
context.fn = this;
var args = [...arguments][1]
var results = context.fn(...args)
delete context
return results
}


bind(thisArg,…)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
Function.prototype.mybind = function(context){
if( typeof this !== 'function'){
throw new TypeError('this not function')
}
var _this = this
//context = context || window
//context.fn = this
var args = [...arguments].slice[1]
return function F(){
if(this instanceof F){
return new _this(...arg, ...arguments)
}else{
_this.apply(context, [...args].concat(arguments))
}
}
}

instanceof

右边变量的原型存在于左边变量的原型链上

1
2
3
4
5
6
7
8
9
10
11
12
13
function(left, right){
var l = left.__proto__
var r = right.prototype
while(true){
if(l === undefined){
return false
}
if(l === r){
return true
}
l = l.__proto__
}
}


Object.create

将传入对象作为原型

1
2
3
4
5
function create(obj){
function F(){}
F.prototype = obj
return new F()
}


new 本质

浅拷贝

深拷贝

继承