1.变量作用域:
function User(){ this.name="Vicfeel"; var age=23; this.sayAge=function(){ console.log('my age id '+age); }}var user = new User();console.log('user.name:'+user.name);console.log('user.age:'+user.age);user.sayAge();
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
2.利用hash将数组去重
Array.prototype.unique = function () {var hash = {}, result = [], type = '', item; for (var i = 0; i < this.length; i++) { item = this[i]; //通过Object.prototype.toString方法,判断某个对象值属于哪种内置类型 type = Object.prototype.toString.call(item); if ( !hash[item + type] ) { hash[item + type] = true; result.push(item); }} console.log(result) return result;};var arr = [0, 1, '1', true, 5, 5,1, true, false, undefined, undefined, null, null];arr.unique();
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16