FE-Interview icon indicating copy to clipboard operation
FE-Interview copied to clipboard

Day36:写出执行结果,并解释原因

Open Genzhen opened this issue 5 years ago • 3 comments

function fn() {
    getValue = function () { console.log(1); };
    return this;
}
fn.getValue = function () { console.log(2);};
fn.prototype.getValue = function () {console.log(3);};
var getValue = function () {console.log(4);};
function getValue() {console.log(5);}
 
//请写出以下输出结果:
getValue();
fn().getValue();
getValue();
new fn.getValue();
new fn().getValue();

Genzhen avatar Jul 06 '20 12:07 Genzhen

扫描下方二维码,获取答案以及详细解析,同时可解锁800+道前端面试题。

Genzhen avatar Jul 06 '20 12:07 Genzhen

后面没有答案解析了

LiSiQinF avatar Aug 11 '21 07:08 LiSiQinF

function fn() { getValue = function () { console.log(1); }; return this; } fn.getValue = function () { console.log(2); }; fn.prototype.getValue = function () { console.log(3); }; var getValue = function () { console.log(4); }; function getValue() { console.log(5); }

//请写出以下输出结果: getValue(); //函数表达式优先,所以是4 fn().getValue(); //1 fn返回的是window,getValue没有写let 或者var是全局的,也就是window getValue(); //1 同上理 new fn.getValue(); //2 实际上new的是getValue new fn().getValue(); //3 继承于prototype

OceanJames avatar Feb 06 '24 02:02 OceanJames