链式操作

image.png
执行完方法把this(对象)return 出去,就可以继续调用

对象属性拼接

  1. obj = {
  2. name: '123'
  3. }
  4. console.log(obj['name']);

image.png

枚举 -> 遍历

对象遍历

  1. var car = {
  2. brand: 'Benz',
  3. color: 'red',
  4. lang: '5',
  5. width: '2.5'
  6. }
  7. for(var key in car){
  8. // consoleg.log(car.key) // undefined
  9. // car.key -> car['key'] -> undefined
  10. console.log(key + ':' + car[key]);
  11. }
  12. for in 一般用来遍历对象的key、for of 一般用来遍历数组的value

hasOwnProperty

找对象自身的属性,排除原型上的自定义的属性
拷贝用得多

  1. var obj = {
  2. name: 'Lisa',
  3. age: 32
  4. }
  5. function Car(){
  6. this.brand = 'Benz',
  7. this.color = 'red'
  8. }
  9. Car.prototype = {
  10. lang: 5,
  11. width: 2.5
  12. }
  13. var car = new Car();
  14. console.log(car);
  15. for(var key in car){
  16. console.log(key + ':' + car[Key]); // 只要是自定义的属性都能打印出来,包括原型链
  17. // 只想打印自己的属性,不打印原型链上的
  18. if(car.hasOwnProperty(key)){
  19. console.log(key + ':' + car[Key]);
  20. }
  21. }

instanceof

A instanceof B A是否是由B构造出来的

  1. function Car(){}
  2. var car = new Car();
  3. console.log(car instanceof Car) // true
  4. console.log(car instanceof Object) // true
  5. console.log([] instanceof Array) // true
  6. console.log([] instanceof Object) // true
  7. console.log({} instanceof Object) // true
  8. A对象的原型里到底有没有B的原型

判断是否是数组
image.png

  1. var str = Object.prototype.toString,
  2. trueTip = '[object Array]';
  3. if(str.call(a) === trueTip){
  4. conosle.log('是数组');
  5. }else{
  6. conosle.log('不是数组');
  7. }
  1. toString重写
  2. var arr = new Array(1,2,3);
  3. console.log(arr.toString()); // 1,2,3
  4. console.log(Object.prototype.toString.call(arr)); // [object Array]

this

  1. // this
  2. // 普通函数内部的this
  3. function test(b){
  4. this.d = 3; // window.d = 3
  5. var a = 1;
  6. function c(){}
  7. }
  8. test(123);
  9. conosle.log(this.d);
  10. AO={
  11. arguments: [123]
  12. this: window
  13. b: 123
  14. a: undefined
  15. c: function c(){}
  16. }
  17. 构造函数
  18. function Test(){
  19. var this = {
  20. __proto__: Test.prototype
  21. }
  22. this.name = '123';
  23. }
  24. var test = new Test(); // new 执行
  25. AO = {
  26. // this: window // 预编译
  27. this: { // 执行后
  28. name: '123',
  29. __proto__: Test.prototype
  30. }
  31. }
  32. GO = {
  33. Test: function test(){...},
  34. test: {
  35. name: '123',
  36. __proto__: Test.prototype
  37. } // 全局
  38. }
  39. // call / apply
  40. function Person(name,age){
  41. this.name = name;
  42. this.age = age;
  43. }
  44. function Programmer(){
  45. Person.apply(this,[name,age]);
  46. this.work = 'Programming'
  47. }
  48. var p = new Programmer('zhangsan',18);
  49. this指向总结
  50. 全局this -> window
  51. 预编译函数this -> window
  52. apply/call改变this指向
  53. 构造函数的this指向实例化对象

caller /callee

  1. function test(a,b,c){
  2. console.log(arguments.callee.length) // 3 arguments.callee相当于test函数
  3. console.log(test.length); //3 形参长度
  4. console.log(arguments.length); //3 实参长度
  5. }
  6. test(1,2,3);
  7. // 找不到函数名(匿名函数),可用arguments.callee
  8. var sum = (function(n){
  9. if(n<1) {return 1};
  10. return n + arguments.callee(n-1);
  11. })(10);
  12. // caller 返回调用当前函数的函数,打印谁在调用这个函数
  13. test1();
  14. function test1(){
  15. test2();
  16. }
  17. function test2(){
  18. console.log(test2.caller); // 谁调用test2,打印谁,严格模式下会报错
  19. }

题目

  1. function foo(){
  2. bar.apply(null, arguments); // 没有改变this指向
  3. // bar(arguments)
  4. }
  5. function bar(){
  6. console.log(arguments);
  7. }
  8. foo(1,2,3,4,5); // [1,2,3,4,5]
  9. bar() -> bar.call(arguments) -> bar(arguments)
  1. JS的typeof可能返回的值有哪些?
  2. object(null) / boolean / number / string / undefined / function
  3. function b(x,y,a){
  4. arguments[2] = 10;
  5. alert(a); // 10
  6. a = 10;
  7. alert(arguments[2]); // 10 形参和实参一一对应关系,谁改对方也改
  8. }
  9. b(1,2,3);
  10. var f = (
  11. function f() {
  12. return '1';
  13. },
  14. function g() {
  15. return 2;
  16. }
  17. );
  18. (f(), g()) // 逗号运算符 返回g() 没有执行
  19. console.log(typeof f); // function
  20. undefined == null // true
  21. undefined === null // false
  22. isNaN('100') // false isNaN('100') 隐式转换 isNaN(Number('100'))
  23. parseInt('1a') == 1 // true
  24. {} == {} // false 引用值对比地址
  25. var obj = {}
  26. obj1 = obj
  27. obj == obj1 // true]
  28. var a = '1';
  29. function test(){
  30. var a = '2';
  31. this.a = '3';
  32. conosle.log(a);
  33. }
  34. test(); // 2
  35. new test(); // 2
  36. console.log(a); // 3