第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問題,去搜搜看,總會(huì)有你想問的

new 關(guān)鍵字 return funciton 問題

new 關(guān)鍵字 return funciton 問題

九州編程 2019-02-18 13:12:16
function people(word){    this.word = word;    this.say = function(){      console.log(this.word);    }    this.capacity = function(){      return this.say;    }  }  var p = new people('Hello World');  var res = p.capacity();  console.log(res)//? (){console.log(this.word);}  console.log(res())//undefined如上帶嗎,我new了一個(gè)people,返回的res 是一個(gè)function但是為什么 我執(zhí)行這個(gè)res為undefined,求解,我想的應(yīng)該打印出來 hello world如果改成這樣呢function people(word){this.word = word;this.say = function(){  console.log(this.word);}()this.capacity = function(){  return this.say;}}var p = new people('Hello World');var res = p.capacity(); //undefined為什么res是undefined
查看完整描述

1 回答

?
jeck貓

TA貢獻(xiàn)1909條經(jīng)驗(yàn) 獲得超7個(gè)贊

好像回答的很晚。你return一個(gè)this.say,而this.say在this.capacity中構(gòu)成了一個(gè)嵌套函數(shù),而在js里嵌套函數(shù)中this是指向window,window中沒有window.word,所以為undefined。

我認(rèn)為把this保存在that里即可:


function people(word){

        var that=this;

        this.word = word;

        this.say = function(){

        console.log(that.word);

    }

        this.capacity = function(){

        return this.say;

    }

  }


查看完整回答
反對(duì) 回復(fù) 2019-02-20
  • 1 回答
  • 0 關(guān)注
  • 389 瀏覽
慕課專欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)