1. this
function test () {} // Function Decaartion
const test = function () {} // Function Expression
const test = () => { // Arrow Function
// 稳定的this指向
}
// 对象是一个容器, 不存在作用域
var obj = {
test: () => {
console.log(this);
const test2 = () => {
console.log(this);
}
}
}
obj.test(); // window
/**
* 内部的xx
*
* Internal Property 属性
* Internal Slots 插槽
* Internal Methods 方法
*
* -> 格式 [[]]
* -> 比如 [[Call]] -> 内部方法 -> 函数执行内部调用方法
* -> [[Constructor]] -> traget -> function instance -> new Target,
*
* new -> 调用以下方法, 有该方法, 才可以执行
* [[Constructor]] (argumentsList, fucntion instance)
*
*/
// 箭头函数不是一个构造器
const test = () => {}
const t = new test(); // Uncaght TypeError: test is not a constructor
2. Array.from
Array.from()
方法对一个类数组, 和可迭代的对象创建一个新的浅拷贝的数组实例.
var obj = {
0: 1,
1: 2,
2: 3,
3: 4,
4: 5,
5: 6,
length: 6
}
// 返回一个数组, 奇数项, 每一项加上num
Array.from(obj. function (item, index) {
if (index % 2 === 0) {
return item + this.num
}
}, {
num: 500
})
3. Temp_space
var a = 1;
// 参数有自己的空间, 参数作用域 -> Temp_space
function test (a, b = function () { a = 2; console.log('--First--', a)}) {
var a = 3;
b();
console.log('--Second--', a);
}
test(100);
console.log('--Third--', a);
// --First-- 2
// --Second-- 3
// --Third-- 1
4. Object.defineProperty, Object.defineProperties
// 定义一个对象, c属性不能被枚举
// {
// a: 1,
// b: 2,
// c: 3
// }
/**
* 默认值: false
* writeable
* configurable
* enumerable
*/
var obj = {};
Object.defineProperties(obj, {
a: {
value: 1,
enumerable: true
},
b: {
value: 2,
enumerable: true
},
c: {
value: 3
}
})
for (let key in obj) {
console.log(key);
}
const arr = Object.keys(obj);
console.log(arr);
// Object.getOwnPropertyNames() 方法返回一个由指定对象的所有自身属性的属性名
// (包括不可枚举属性, 但不包括 Symbol 值作为名称的属性) 组成的数组.
const arrInner = Object.getOwnPropertyNames(obj);
console.log(arrInner);