示例1
?let obj = {
? ? ? ? ? ? name:'張三',
? ? ? ? ? ? age:20,
? ? ? ? ? ? sayHi(){
? ? ? ? ? ? ? ? return{
? ? ? ? ? ? ? ? ? ? // 箭頭函數(shù)中的this域携,指向外層方法中的this
? ? ? ? ? ? ? ? ? ? sayHello:()=>{
? ? ? ? ? ? ? ? ? ? ? ? console.log(this.name,this.age);
? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? }
? ? ? ? ? ? },
? ? ? ? ? ? girl:{
? ? ? ? ? ? ? ? // 注意:由于箭頭函數(shù)中的this灯抛,執(zhí)行外層方法中的this耸别,當(dāng)前箭頭函數(shù)的外層已經(jīng)沒有方法了
? ? ? ? ? ? ? ? // 所以存淫,this指向window
? ? ? ? ? ? ? ? // 所以瞳收,這個(gè)方法就不適合定義成箭頭函數(shù)插爹,改成普通函數(shù)情妖,
? ? ? ? ? ? ? ? // 調(diào)用該函數(shù)時(shí)采用call方法修改當(dāng)前方法里面的this指向
? ? ? ? ? ? ? ? sayHello:function(){
? ? ? ? ? ? ? ? ? ? console.log('我的男朋友是'+this.name+','+this.age);
? ? ? ? ? ? ? ? }
? ? ? ? ? ? }
? ? ? ? }
? ? ? ? // 注意:obj.sayHi()返回的是對(duì)象码秉,再去.sayHello()方法颠悬,
? ? ? ? // sayHello()方法里面的this指向obj.sayHi()放回的是對(duì)象
? ? ? ? obj,sayHi().sayHello()
? ? ? ? obj.girl.sayHello.call(obj)
示例2
?// var 定義的成員矮燎,會(huì)自動(dòng)添加為window對(duì)象的成員
? ? ? ? var address = '安德門'
? ? ? ? var showAddress = function(){
? ? ? ? ? ? console.log(`地點(diǎn)在${this.address}`);
? ? ? ? }
? ? ? ? let obj1 = {
? ? ? ? ? ? name:'鹿晗',
? ? ? ? ? ? age:30,
? ? ? ? ? ? sayHi:function(){
? ? ? ? ? ? ? ? console.log(`Hi!我叫${this.name},今年${this.age}歲`);
? ? ? ? ? ? },
? ? ? ? ? ? showMyFriend:function(){
? ? ? ? ? ? ? ? return {
? ? ? ? ? ? ? ? ? ? name:'關(guān)曉彤',
? ? ? ? ? ? ? ? ? ? age:20,
? ? ? ? ? ? ? ? ? ? sayHi(){
? ? ? ? ? ? ? ? ? ? ? ? console.log(`Hi!我叫${this.name},今年${this.age}歲`);
? ? ? ? ? ? ? ? ? ? },
? ? ? ? ? ? ? ? ? ? showLh:()=>{
? ? ? ? ? ? ? ? ? ? ? ? console.log(`Hi!我的男朋友是${this.name},今年${this.age}歲`);
? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? }
? ? ? ? ? ? }
? ? ? ? }
? ? ? ? obj1.sayHi()
? ? ? ? let obj2 = obj1.showMyFriend()
? ? ? ? obj2.sayHi()
? ? ? ? obj2.showLh()
示例3
?// 構(gòu)造函數(shù)不能使用箭頭函數(shù)定義
? ? ? ? // 構(gòu)造函數(shù)里面的this,用于給類定義成員(屬性和方法)
? ? ? ? function Person(name,age){
? ? ? ? ? ? this.name = name
? ? ? ? ? ? this.age = age
? ? ? ? ? ? this.sayHI = function(){
? ? ? ? ? ? ? ? console.log(`大家好赔癌!我是 ${this.name},今年${this.age}歲`);
? ? ? ? ? ? }
? ? ? ? }
? ? ? ? let p1 = new Person('張三',20)
? ? ? ? console.log(p1);
? ? ? ? let obj1 = {
? ? ? ? ? ? name:'肖戰(zhàn)',
? ? ? ? ? ? age:20,
? ? ? ? ? ? sayHI:function(){
? ? ? ? ? ? ? ? // 方法里面的this诞外,指向方法的調(diào)用者
? ? ? ? ? ? ? ? // 如果一個(gè)方法,不是由對(duì)象調(diào)用執(zhí)行的灾票,而是直接執(zhí)行的峡谊,那么該方法里面的this就執(zhí)行window對(duì)象
? ? ? ? ? ? ? ? console.log(`Hi!我是 ${this.name},今年${this.age}歲`);
? ? ? ? ? ? },
? ? ? ? ? ? sayHello:()=>{
? ? ? ? ? ? ? ? // 箭頭函數(shù)中沒有this刊苍,如果在箭頭函數(shù)中使用了this既们,會(huì)向外層尋找this的指向
? ? ? ? ? ? ? ? // 如果所有的外層都沒有this,最終會(huì)指向window對(duì)象
? ? ? ? ? ? ? ? console.log(`Hello正什!我是 ${this.name},今年${this.age}歲`);
? ? ? ? ? ? }
? ? ? ? }
? ? ? ? obj1.sayHI()
? ? ? ? let obj2 = {
? ? ? ? ? ? name:'關(guān)曉彤',
? ? ? ? ? ? age:20
? ? ? ? }
? ? ? ? // 可以將obj1的函數(shù)傳給obj2啥纸,其實(shí)是obj2的sayHI方法,指向obj1的方法
? ? ? ? obj2.sayHI = obj1.sayHI
? ? ? ? obj2.sayHI()
? ? ? ? // 將obj1身上的函數(shù)婴氮,傳給了一個(gè)sayHI變量
? ? ? ? let sayHI = obj1.sayHI
? ? ? ? window.name = '小明'//設(shè)置window對(duì)象的name屬性
? ? ? ? window.age = 20//設(shè)置window對(duì)象的age屬性
? ? ? ? sayHI()
? ? ? ? console.log('-----------------');
? ? ? ? obj1.sayHello()
? ? ? ? obj2.sayHello = obj1.sayHello
? ? ? ? obj2.sayHello()
? ? ? ? let sayHello = obj1.sayHello
? ? ? ? sayHello()