| 学习时间 |
完成时间 ✅ |
重难点 |
疑问/遗漏 |
|
|
|
|
| 复习时间 |
|
|
|
|
|
|
对象的遍历
var sched={ wakeup: function(){ console.log('Running') return this }, morning: function(){ console.log('shopping') return this }}sched.wakeup().morning()对象的枚举 有遍历也就有枚举var car={ brand: 'Benz', color:'red', displacement:'3.0'}for(var key in car){ //for in同样可以遍历数组,for in会遍历出来原型上自定义的属性 console.log(car.key) //此处会打印undefined,因为javaScript内部的处理是 //car.key->car['key']->undefined console.log(car[key])}
hasOwnProperty
function Car(){ this.brand='Benz'; this.color='red';}Car.prototype={ land: 5, width: 2.5}Object.prototype.name='Object'var car=new Car();'land' in car //truefor(var key in car){ console.log(key) //brand color land width name 也会遍历出自定义的原型属性 if(car.hasOwnProperty(key)){ //排除自定义原型上的属性 console.log(car[key]) //Benz red }}
intanceof
function Car(){}var car=new Car();console.log(car instanceof Car) //trueconsole.log(car instanceof Object) //trueconsole.log(car instanceof Array) //trueA对象的原型里到底有没有B的原型var arr=[];var str=Object.prototype.toString; //判断数据类型的方式if(str.call(a)==='[object Array]'){ console.log('是数组')}else{ console.log('不是数组')}
this
函数内部的thisfunction test(b){ this.d='3';//相当于window.d=3; this和window一样 var a=1; function c(){}}test(123)console.log(d) //'3'构造函数function Test(){ this.name='123' //当构造函数实例化的时候,this就指向了test}var test=new Test();call和apply第一个参数是啥this就指向啥总结:全局this指向window预编译函数this->windowapply/call改变this指向构造函数的this指向实例化对象
callee/caller
function test(a,b,c){ console.log(arguments.callee.length)相当于test.length,arguments.callee表示函数本身}function sum(n){ if(n<=1){ return 1; } return n+sum(n-1)}var sum=(function(n){ if(n<=1){ return 1; } return n+sum(n-1);})(100)console.log(sum)caller可以检测出谁调用它了
试题
function foo(){ bar.apply(null,arguments)}function bar(){ console.log(arguments) //Arguments(2) [1, 3, callee: ƒ, Symbol(Symbol.iterator): ƒ]}foo(1,3) typeof可以返回的值:object、boolean、number、string、undefined、functionvar f=( function f(){ return '1' }, function g(){ return 2 }//逗号表达式,返回的是g函数)();console.log(typeof(f)); //numberconsole.log({}=={})//falsevar a='1';function test(){ var a='2'; this.a='3'; console.log(a)}test(); //2new test(); //2console.log(a)//3