链式操作

执行完方法把this(对象)return 出去,就可以继续调用
对象属性拼接
obj = {name: '123'}console.log(obj['name']);

枚举 -> 遍历
对象遍历
var car = {brand: 'Benz',color: 'red',lang: '5',width: '2.5'}for(var key in car){// consoleg.log(car.key) // undefined// car.key -> car['key'] -> undefinedconsole.log(key + ':' + car[key]);}for in 一般用来遍历对象的key、for of 一般用来遍历数组的value
hasOwnProperty
找对象自身的属性,排除原型上的自定义的属性
拷贝用得多
var obj = {name: 'Lisa',age: 32}function Car(){this.brand = 'Benz',this.color = 'red'}Car.prototype = {lang: 5,width: 2.5}var car = new Car();console.log(car);for(var key in car){console.log(key + ':' + car[Key]); // 只要是自定义的属性都能打印出来,包括原型链// 只想打印自己的属性,不打印原型链上的if(car.hasOwnProperty(key)){console.log(key + ':' + car[Key]);}}
instanceof
A instanceof B A是否是由B构造出来的
function Car(){}var car = new Car();console.log(car instanceof Car) // trueconsole.log(car instanceof Object) // trueconsole.log([] instanceof Array) // trueconsole.log([] instanceof Object) // trueconsole.log({} instanceof Object) // trueA对象的原型里到底有没有B的原型
判断是否是数组
var str = Object.prototype.toString,trueTip = '[object Array]';if(str.call(a) === trueTip){conosle.log('是数组');}else{conosle.log('不是数组');}
toString重写var arr = new Array(1,2,3);console.log(arr.toString()); // 1,2,3console.log(Object.prototype.toString.call(arr)); // [object Array]
this
// this// 普通函数内部的thisfunction test(b){this.d = 3; // window.d = 3var a = 1;function c(){}}test(123);conosle.log(this.d);AO={arguments: [123]this: windowb: 123a: undefinedc: function c(){}}构造函数function Test(){var this = {__proto__: Test.prototype}this.name = '123';}var test = new Test(); // new 执行AO = {// this: window // 预编译this: { // 执行后name: '123',__proto__: Test.prototype}}GO = {Test: function test(){...},test: {name: '123',__proto__: Test.prototype} // 全局}// call / applyfunction Person(name,age){this.name = name;this.age = age;}function Programmer(){Person.apply(this,[name,age]);this.work = 'Programming'}var p = new Programmer('zhangsan',18);this指向总结全局this -> window预编译函数this -> windowapply/call改变this指向构造函数的this指向实例化对象
caller /callee
function test(a,b,c){console.log(arguments.callee.length) // 3 arguments.callee相当于test函数console.log(test.length); //3 形参长度console.log(arguments.length); //3 实参长度}test(1,2,3);// 找不到函数名(匿名函数),可用arguments.calleevar sum = (function(n){if(n<1) {return 1};return n + arguments.callee(n-1);})(10);// caller 返回调用当前函数的函数,打印谁在调用这个函数test1();function test1(){test2();}function test2(){console.log(test2.caller); // 谁调用test2,打印谁,严格模式下会报错}
题目
function foo(){bar.apply(null, arguments); // 没有改变this指向// bar(arguments)}function bar(){console.log(arguments);}foo(1,2,3,4,5); // [1,2,3,4,5]bar() -> bar.call(arguments) -> bar(arguments)
JS的typeof可能返回的值有哪些?object(null) / boolean / number / string / undefined / functionfunction b(x,y,a){arguments[2] = 10;alert(a); // 10a = 10;alert(arguments[2]); // 10 形参和实参一一对应关系,谁改对方也改}b(1,2,3);var f = (function f() {return '1';},function g() {return 2;});(f(), g()) // 逗号运算符 返回g() 没有执行console.log(typeof f); // functionundefined == null // trueundefined === null // falseisNaN('100') // false isNaN('100') 隐式转换 isNaN(Number('100'))parseInt('1a') == 1 // true{} == {} // false 引用值对比地址var obj = {}obj1 = objobj == obj1 // true]var a = '1';function test(){var a = '2';this.a = '3';conosle.log(a);}test(); // 2new test(); // 2console.log(a); // 3
