关于函数名和var声明的变量名相同的情况
function a() {
console.log(10);
}
var a;
console.log(a);
a();
a = 3;
- 1、函数和var声明的变量作用域会发生提升
- 2、han’shu提升会优先与变量提升
- 3、如果函数和变量名一样,并且变量没有被赋值,则函数生效
this相关
- 1、被调用的函数,点前面是谁this就是谁,否则为window
this.a = 20;
var test = {
a: 40,
init() {
console.log(this);
console.log(this.a);
}
}
test.init();
var fn = test.init;
fn();
- 2、在闭包的情况下,没有函数去调用go(),所以this的上下文就是this
this.a = 20;
var test = {
a: 40,
init() {
function go() {
console.log(this);
console.log(this.a);
}
go();
}
}
test.init();
- 3、箭头函数中的this只取决于包裹箭头函数的的第一个普通函数的this
this.a = 20;
var test = {
a: 40,
init: () => {
console.log(this);
console.log(this.a);
}
}
test.init();
function可以new,es6中的简写方式不可以new
var s = {
a: function() {
console.log(1)
},
b() {
console.log(2)
}
}
var f = s.a.bind(this)
new f()
var p = s.b.bind(this)
new p()
当使用new创建函数时,this指向当前函数的实例
var s = {
a: function() {
console.log(this)
}
}
var f = s.a.bind(this)
new f()
如何将一个字符串变为一个数组
Array.from("abc");
[...new Set("abc")];
"abc".split("");
只有new一个函数的时候,在函数上找不到才会去原型链上找
function test() {}
test.prototype.a = 1;
test.a
function test() {}
test.prototype.a = 1;
var test_ = new test();
test_.a;