Skip to content

JS差缺补漏 #44

Open
Open
@L-small

Description

@L-small

1、

var a = 1;
if(true){
    console.log(a);
    let a = 2;
}

由于let的创建,所以这里的a应该是报错

2、

(function() {
  var a = b = 3;
})();
console.log(typeof a === 'undefined');
console.log(typeof b === 'undefined');

typeof 报错不会抛出而是等于undefined

3、

function f(){
    return f;
}
console.log(new f() instanceof f);

当new的构造函数返回值是函数的时候,也是同样返回函数,而不是只有对象的时候才返回对象

4、

console.log(1 + -"1" + "2");
console.log( "A" - "B" + "2"); 
console.log( "A" - "B" + 2); 

当用-'1' === -1,所以是'02'。'A' - 'B'则是NaN。NaN + 任何数字 === NaN

5、

var x = 1;
if(function f(){}){
    x += typeof f;
}
 
console.log(x);

(function f(){})的f在括号中并不会函数声明提升。所以f访问不到而typeoof f变成了undefined

6、

Object.prototype.bar = 1; 
var foo = {
    goo: undefined
};

console.log(foo.bar);
console.log('bar' in foo);

console.log(foo.hasOwnProperty('bar'));
console.log(foo.hasOwnProperty('goo'));

in会去查找原型链上的属性

7、

function foo1() {
    return {
        bar: "hello"
    };
}
function foo2() {
    return 
    {
        bar: "hello"
    };
}
console.log(foo1());
console.log(foo2());

当换行后,会自动的添加;号,变成return;

8、

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

变量提升也有优先级, 函数声明 > arguments > 变量声明

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions