美文网首页工作生活
形参与变量声明和函数声明同名(自测原型链)

形参与变量声明和函数声明同名(自测原型链)

作者: 翩翩公子银圈圈 | 来源:发表于2019-07-03 13:03 被阅读0次

题目来源:https://www.cnblogs.com/simonbaker/p/4237970.html
九道题目还是很有纪念价值的:
第一道:

a()
var a = c = function() {
    console.log(2)
}
a()

function a() {
    console.log(1)
}
a();
(function(b) {
    b(), c()
    var b = c = function a() {
        console.log(3)
    }
    b()
})(a)
c()

答案:1 2 2 2 2 3 3
解析:这个可以放在最后解析

var A = function() {}
A.prototype.n = 1;

var b = new A()

A.prototype = {
    n: 2,
    m: 3
}

var c = new A()
console.log(b.n, b.m, c.n, c.m)

答案:1 undefined 2 3
解析:在b被实例化的时候只是有A的变量表达式,给了A的原型对象一个n的属性,此时m属性为定义,因此b.m=undefined;类似原理,在c被实例化的时候,A的原型对象被重新赋值,因此c.n=2,c.m=3

(function f() {
    function f() {
        return 1;
    }
    return f();

    function f() {
        return 2;
    }
})();

答案:2
解析:同名函数声明,后面的函数声明替换前面的函数声明,因此返回的f()是第二个声明的函数

if (!a) {
    var a = 1;
}
console.log(a)

答案: 1
解析:这个是不是很简单,因为js没有块级作用域呀。
拓展一下:

if (!(a in window)) {
    var a = 1;
}
console.log(a)

变量提升,因此首先a在window中满足条件,因此赋值语句无法执行,因此最后打印出来是undefined。
如果此时var变为let,那么无法打印a,因为let是可以形成块级作用域呀,无法提升变量!!!


image.png

5

function a() {}
var a;
console.log(typeof a)

答案:function
解析:只要记住一条就可以了,函数声明 替换 变量声明(记住不要把变量声明理解为变量赋值哦)
6

(function(b) {
    console.log(b)
    var b = c = 2
    console.log(b)
})(1)

答案:1 2
解析:

(function (b) {
    var b = 1//所谓的传入参数的过程就是声明加赋值
    console.log(b)
    var b = c = 2
    console.log(b)
})(1)

7

(function(b) {
    console.log(b)
    var b = c = 2
    console.log(b)

    function b() {}
    console.log(b)
})(1)

答案:f b(){} 2 2
解析:换一种写法就很清楚了

(function(b) {
    var b=1//参数定义搁在最前面
    function b() {}//函数声明提升,不好意思,把上面的覆盖了
    console.log(b)//打印的就是函数喽
    var b = c = 2
    console.log(b)//2
    console.log(b)//2
})(1)
var a = 10;

function fn() {
    console.log(1)
    var a = 100;
    console.log(a)
}
fn();

答案:1 100
解析:这个要是不知道,那就去看书吧
9

var a = 1

function c(a, b) {
    console.log(a)
    a = 2
    console.log(a)
}
c()

答案:undefined 2
解析:

var a = 1

function c(a, b) {
    console.log(a)
    a = 2
    console.log(a)
}
c()
console.log(a)

打印一下a就会发现是1呀,所以参数如果和外部的变量同名,不要去纠结,把参数当做另一个字母表示就可以了

在讲解题目的时候,先贴两份代码,

当传入参数为函数声明的时候:

function f(a) {
    console.log(a);  // ƒ a() {  console.log('outer'); }
    var a = 2;
    console.log(a);  // 2
}
f(a);
function a() {
    console.log('outer');
}

换个清楚的:

function f(a) {
    function a() {
      console.log('outer');
    }
    console.log(a);  // ƒ a() {  console.log('outer'); }
    var a = 2;
    console.log(a);  // 2
}
f(a);
function a() {
    console.log('outer');
}

再来一个

function f(a) {
    console.log(a);  // ƒ a() { }
    var a = 2;
    function a() {}
    console.log(a);  // 2
}
f(a);
function a() {
    console.log('outer');
}

擦擦眼睛:

function f(a) {
function a() {
    console.log('outer');
}
    function a() {}
    console.log(a);  // ƒ a() { }
    var a = 2;
    console.log(a);  // 2
}
f(a);
function a() {
    console.log('outer');
}

第二类情况:传入函数赋值的变量


function f(a) {
    console.log(a);  // function a() {}
    var a = 2;
    function a() {}
    console.log(a);  // 2
}
f(a);
var a = function b() {
    console.log('outer');
}

擦擦眼睛:

function f(a) {
    var a = function b() {
      console.log('outer');
    }
    function a() {}
    console.log(a);  // function a() {}
    var a = 2;
    console.log(a);  // 2
}
f(a);
var a = function b() {
    console.log('outer');
}

ok!!再回头看第一题是不是很好理解了
解析:

function a() {
    console.log(1)
} //函数声明提升
a() //第一个a()打印出来就是1
var a = c = function () {
    console.log(2)
} //给a,c重新赋值
a() //第二个a执行因此为2

a(); //第三个a执行因此为2
(function (b) { //b其实就是a啊
    b(), c() //a没有改变,当然打印2呀,c又不是参数,c根据作用域链,发现了父级的作用域链,打印的当然也是2
    var b = c = function a() {
        console.log(3)
    } //记得赋值是从右往左赋值,而且c被重新赋值了,而且c还在window中
    b()
})(a)
c()
a()//2

完结!!!
参考:https://www.cnblogs.com/simonbaker/p/4237970.html
https://www.cnblogs.com/aredleave/p/7596233.html
https://blog.csdn.net/weixin_34273046/article/details/87015464

相关文章

  • 形参与变量声明和函数声明同名(自测原型链)

    题目来源:https://www.cnblogs.com/simonbaker/p/4237970.html九道题...

  • js-预编译

    函数声明整体提升变量 声明提升 function 创建AO对象(Active Object) 寻找形参与变量声明,...

  • 猫眼

    var变量提升,函数声明提升,消除变量声明提升(let); 高阶函数,函数式的编程,柯里化 原型链继承,js面向对...

  • 待整理01-JS 形参与变量声明和函数声明同名怎么办?

    1. 简介 前两天遇到一道很有意思的题目,乍看起来是一道考声明提升和变量提升的题目的,但事实上还包含了更多的知识点...

  • JavaScript深入浅出——函数

    什么是函数 变量和函数的声明会被前置 this的指向 严格模式 作为对象的方法 原型链上的this call/ap...

  • 文件作用域

    在JavaScript文件中声明的变量和函数只在该文件中有效;不同的文件中可以声明相同名字的变量和函数,不会互相影...

  • zj3 函数与作用域

    讲解函数声明、函数表达式、声明前置、作用域、作用域链相关概念 函数声明和函数表达式有什么区别 什么是变量的声明前置...

  • 2019-08-05 拓展现有函数

    定义、声明的函数 拓展原型链上的方法

  • 框架-模块化、API

    文件作用域 在 JavaScript 文件中声明的变量和函数只在该文件中有效;不同的文件中可以声明相同名字的变量和...

  • 关于JS函数声明和变量同名问题

    之前学习JS的变量声明前置和函数声明的相关内容时,忘记讨论一个互相影响的问题了,这里特地补充一下。 即JavaSc...

网友评论

    本文标题:形参与变量声明和函数声明同名(自测原型链)

    本文链接:https://www.haomeiwen.com/subject/bbkqhctx.html