链式操作

  1. var obj = {
  2. morning:function(){
  3. console.log('早上');
  4. return this;
  5. },
  6. noon:function(){
  7. console.log('中午');
  8. return this;
  9. },
  10. night:function () {
  11. console.log('晚上');
  12. return this;
  13. }
  14. }
  15. obj.morning().noon().night();//早上 中午 晚上

对象枚举

  1. var arr = [1, 2, 3, 4, 5];
  2. var car = {
  3. brand: 'Benz',
  4. color: 'red',
  5. }
  6. for (var key in car) {
  7. //console.log(car.key);//undefined;
  8. // car.key => car['key'];//报错
  9. console.log(car[key])//
  10. }
  11. for (var key in arr) {
  12. console.log(arr[key]);//1,2,3,4,5
  13. console.log(key);// 0,1,2,3,4
  14. }

hanOwnProperty

  1. 对象自身属性中是否包含指定的属性,有返回true 没有返回false
  2. for in 遍历会遍历整个对象包括原型上的属性 ```javascript // 申明构造函数Car。 function Car() { this.brand = ‘Benz’; this.color = ‘red’; this.displacement = ‘3.0’; }

//给Car的prototype重新赋值 Car.prototype = { lang: 5, width: 2 } // 实例化构造函数 var car = new Car();

//for in 循环实例化对象 for (var k in car) { console.log(car[k]);//此时会输出原型链上的lang 和width; } for(var k in car){ //判断car中是否包含k。 if (car.hasOwnProperty(k)) { console.log(car[k]);//此时没有输出原型的属性。 } }

  1. ![image.png](https://cdn.nlark.com/yuque/0/2021/png/12831495/1616657382963-5d23157e-8a11-4909-8454-37617d2abcd8.png#align=left&display=inline&height=115&margin=%5Bobject%20Object%5D&name=image.png&originHeight=115&originWidth=559&size=5911&status=done&style=none&width=559)<br />![image.png](https://cdn.nlark.com/yuque/0/2021/png/12831495/1616658107826-ac0e1d39-4d60-429c-a5ad-6df66fc1e87c.png#align=left&display=inline&height=114&margin=%5Bobject%20Object%5D&name=image.png&originHeight=114&originWidth=458&size=4700&status=done&style=none&width=458)
  2. <a name="X2Dw6"></a>
  3. ## instanceof
  4. _判断对象是否是该构造函数构造出来的。_
  5. ```javascript
  6. function Car() { };
  7. //实例化Car
  8. var car = new Car();
  9. //声明Person对象
  10. function Person() { };
  11. //实例化person
  12. var person = new Person();
  13. console.log(car instanceof Car);//true
  14. console.log(car instanceof Person);//false
  15. console.log([] instanceof Object);//true;数组也是由Object构造出来的
  16. console.log(car instanceof Object);//true;对象的原型链也包含了Object
  17. console.log([] instanceof Array);//true;
  18. console.log({} instanceof Object);//true;

A 对象的原型里到底有没有B的原型。

  1. var a = [];
  2. console.log(a.constructor);//数组的constructor是Array方法
  3. console.log(a instanceof Array); //true a是Array的实例。
  4. var str = Object.prototype.toSting.call(a);//
  5. console.log(str)//[object,Array];

this

函数内部this

  1. 预编译函数this指向window;
  2. 全局函数this指向window
  3. apply/call改变this 指向
  4. 构造函数的this 被修改成了指向实例化对象
  5. 函数执行的时候 谁调用了这个函数这个函数就指向谁。
    1. function Test() {
    2. /**
    3. * var this = {
    4. * __proto__:Test.prototype
    5. * }
    6. */
    7. this.name = 123;
    8. //return this;
    9. }
    10. var test = new Test();
    11. /**
    12. * AO {
    13. * this: window->{
    14. * __proto__:Test.prototype
    15. * }
    16. * }
    17. *
    18. * GO {
    19. * this -> window
    20. * test -> undefined
    21. * Test ->function Test(){};
    22. * }
    23. */

    caller callee

    例子1

    1. function test(a, b, c) {
    2. console.log(arguments.callee.length);
    3. //形参个数
    4. console.log(test.length);
    5. //形参个数
    6. console.log(arguments.length);
    7. //返回实参个数
    8. }
    9. test(1,2,3)

    例子2

    ```javascript //利用arguments.callee求0到n的和。 var sum = (function (n) { if (n <=1) {
    1. return 1
    } return n + arguments.callee(n - 1);//arguments.callee 指向函数本身。 })(10) //递归方法 function sum(n) { if (n <= 1) {
    1. return 1;
    } return n+sum(n-1) }
  1. <a name="xp7OY"></a>
  2. ## 面试题:
  3. <a name="IxRFC"></a>
  4. ### 题目1
  5. 在浏览器环境下。当apply 或者call 的时候第一个参数如果是undefined/null的时候、someFunction的this永远指向window;
  6. ```javascript
  7. someFunction.apply(undefined/null,[])
  8. function someFunction(){
  9. console.log(this)//window
  10. }
  11. function foo() {
  12. bar.apply(null, arguments);
  13. bar.call(arguments);//等同于bar.apply(arguments)
  14. }
  15. function bar() {
  16. console.log(arguments);
  17. }
  18. foo(1, 2, 3, 4, 5);

题目2

typeof 的返回值都有哪些
答:string/number/boolean/undefined/object(null,array)/function。

题目3

  1. var f = (
  2. function f() {
  3. return 1
  4. }
  5. ,
  6. function g() {
  7. return 2
  8. }
  9. )
  10. // 此时f = function g(){ return 2};
  11. typeof(f)// function
  12. typeof(f())//f()执行返回2 所以是number
  13. typeof((f()))//(2)转成了表达式 string

题目4

  1. undefined == null;// null和undefined互相相当 true
  2. undefined === null;//但是不全等。因为各自有各自的类型。false
  3. isNaN('100');//字符串100会转成Number 所以是false
  4. console.log(parseInt('1a')==1);// parseInt('1a')会 转成 1 所以为true

题目5

  1. function isNaN1(num) {
  2. var str = Number(num) + ''//加''是因为NaN不等于NaN;NaN不等于任何数所以NaN只能转成字符串。
  3. if (str == 'NaN') {
  4. return true;
  5. } else {
  6. return false;
  7. }
  8. }

题目6

  1. {}=={}//不等于,因为引用值比较的是地址
  2. //如何让他们相等 只能通过赋值、给地址
  3. var a = {};
  4. b =a;
  5. a==b//true

题目7

  1. var a = '1';
  2. function test() {
  3. var a = '2'; //AO{a:2}
  4. this.a = '3';
  5. console.log(a);
  6. }
  7. test();//2,执行这个的时候this 指向的window; this.a = 3;GO{a->3}
  8. new test();//2,
  9. console.log(a);//3
  10. /**
  11. * GO {
  12. * a:undefined->'1',
  13. * test:function(){....}
  14. * }
  15. * AO{
  16. * a:undefined->'2',
  17. * this:window
  18. * }
  19. */

题目8

  1. var a = 5;
  2. function test() {
  3. a = 0;// AO.a = 0;
  4. console.log(a);// 0
  5. console.log(this.a);test() this指向window.
  6. var a;//变量提升
  7. console.log(a);//0
  8. }
  9. test();//0,5,0
  10. new test();//0,undefined,0;
  11. /***
  12. * 分析
  13. * AO {
  14. * a:undefined->0,
  15. * this:window{a:5}
  16. * }
  17. * GO {
  18. * a->undefined->5
  19. * }
  20. */