渡一笔记

预编译

  1. 创建AO对象
  2. 找形参和变量声明, 将变量和形参名作为AO属性名,值为undefined
  3. 将实参值和形参值统一
  4. 在函数体里面找函数声明,值赋予函数体
1
2
3
4
5
6
7
8
9
10
11
12
13
function test(a, b) {
console.log(a);
c = 0;
var c;
a = 3;
b = 2;
console.log(b);
function b () {}
function d () {}
console.log(b);
}
test(1)

类数组

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
//类数组 属性要为索引(数字)属性, 必须有length属性 最好加上push
var obj = {
'2': 'a',
'3': 'b',
'length': 3,
'push': Array.prototype.push,
'splice': Array.prototype.splice //添加这个属性 对象看起来会像数组一样[]
}

obj.push('c')
console.log(obj)
//类似原理
// Array.prototype.push = function(target) {
// this[this.length] = target;
// this.length ++;
// }

封装typeof

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
//1. 分两类: 原始值 引用值
//2. 区分引用值
function type(target) {
var template = {
"[object Array]": "array",
"[object String]": "string - object",
"[object Number]": "number - object",
"[object Object]": "object",
"[object Boolean]": "boolean - object"
};

if (target === null) {
return 'null';
} else if (typeof target === 'object'){
//对象 和 包装类
var objToString = Object.prototype.toString.call(target);
return template[objToString];
} else {
//原始值和function
return typeof target;
}
}

数组去重

1
2
3
4
5
6
7
8
9
10
11
12
13
//数组去重
Array.prototype.unique = function() {
var temp = {},
arr = [],
len = this.length;
for (var i = 0; i < len; i++) {
if (temp[this[i]]=== undefined) {
temp[this[i]] = 1;
arr.push(this[i])
}
}
return arr;
}