国产成人精品亚洲777人妖,欧美日韩精品一区视频,最新亚洲国产,国产乱码精品一区二区亚洲

您的位置:首頁技術(shù)文章
文章詳情頁

javascript - 求解js 的問題 為什么結(jié)果是5? 分析一下

瀏覽:126日期:2023-02-07 14:10:14

問題描述

<!doctype html><html lang='zh-CN'><head><meta http-equiv='X-UA-Compatible' content='IE=edge'/><meta name='viewport' content='width=device-width,initial-scale=1,maximum-scale=1,user-scalable=no'><meta charset='UTF-8'/><title>Document</title></head><body><script> var test=(function(a){ this.a=a; return function(b){ return this.a+b; } }(function(a,b){ return a; debugger; }(1,2))); console.log(test(4)) //結(jié)果是輸出5 求解? </script></body></html>

問題解答

回答1:

let functionA = function (a) { this.a = a return function (b) {return this.a + b }}let argA = function (a, b) { return a debugger}(1, 2)// 實際上 argA 就等于 1,** 這個地方的 b 沒有被用到 **

則原式簡化成:

let test = functionA(argA)

此句執(zhí)行完后 test 實為

function (b) { return this.a + b}// ** 這是一個帶一個參數(shù)的函數(shù),執(zhí)行 test(4) 時 b 就是 4 **

且此時 this.a 等于 1。因此 test(4) 結(jié)果為 5

回答2:

很顯然是5啊

var test = function(a){ this.a = a; return function(b){return this.a + b; } }(function(a,b){ return a; }(1,2))

分解

var test = function(a){ this.a = a; return function(b){return this.a + b; } }var getA = function(a,b){ return a; }test(getA(1,2))(4);

這要再看不懂,你就要好好學(xué)習(xí)下基礎(chǔ)了

回答3:

首先我們要理解test這個變量,test其實就是一個函數(shù),如下

var test = function(b){ return this.a + b;}

外面那層部分是一個立即執(zhí)行的函數(shù),首先,

function(a,b){ return a; }(1,2)

這部分的結(jié)果就是 1,也就是說,代碼可以簡化為:

var test=(function(a){ this.a=a; return function(b){ return this.a+b; } }(1));

在上面的代碼里面,a=1,因此,在test(4)中,我們得到的是:

var test=(function(a){ // a = 1 this.a=a; return function(b){ // b = 4 return this.a+b; // 得到 1 + 4 } }(1));

標(biāo)簽: JavaScript
相關(guān)文章:
主站蜘蛛池模板: 高平市| 桃江县| 通河县| 临沧市| 沁水县| 察雅县| 陈巴尔虎旗| 本溪| 黎城县| 西林县| 和田市| 张家港市| 高陵县| 榆社县| 宁城县| 衡水市| 阳曲县| 梓潼县| 启东市| 哈巴河县| 河津市| 类乌齐县| 柏乡县| 京山县| 沙洋县| 星座| 镇安县| 永胜县| 瑞昌市| 六枝特区| 墨玉县| 达尔| 汶上县| 乐业县| 集贤县| 新平| 石狮市| 马公市| 太仆寺旗| 白河县| 乌兰察布市|