前端小白成长04--Object.is  this

2020-05-17  本文已影响0人  Clover园

Object.is()确定两个值是否相同 Ie不支持

Object.is('foo', 'foo');     // true
Object.is(window, window);   // true

Object.is('foo', 'bar');     // false
Object.is([], []);           // false

var foo = { a: 1 };
var bar = { a: 1 };
Object.is(foo, foo);         // true
Object.is(foo, bar);         // false

Object.is(null, null);       // true

// Special Cases
Object.is(0, -0);            // false
Object.is(-0, -0);           // true
Object.is(NaN, 0/0);         // true
Object.is(NaN,NaN)        //true

this指向
在箭头函数内是没有 this 指向的,箭头函数里面的 this 是继承于上一级的上下文,只有函数才有执行上下文

let obj = {
  a : 10,
  f1: ()=>{
    console.log(this);//window   this指上层对象。若无定义上层对象,则为window
  },
  f2:function(){
    console.log(this);//{a: 10, f1: ƒ, f2: ƒ, f3: ƒ}
  },
  f3(){
    console.log(this)//{a: 10, f1: ƒ, f2: ƒ, f3: ƒ}
  }
}
obj.f1();
obj.f2();
obj.f3();


const obj = {
    name: 'tom',
    student: {
        name: 'AAAA',
        say: () => {
            console.log('嵌套对象的箭头函数');
            console.log(this);  //window
            console.log(this.name); 
        },
        eat: function () {
            console.log('嵌套对象的普通函数');
            console.log(this); 
            console.log(this.name); 
        }
    }
}
obj.student.say()
obj.student.eat()


上一篇 下一篇

猜你喜欢

热点阅读