js_继承及原型链等(四)

2019-02-08  本文已影响0人  mao77_

js_继承及原型链等(三)

1. 继承


  function Father() {
    this.name = 'eloise'
  }

  Father.prototype.giveMoney = function () {
    console.log('im eloise, hello');
  }

  function Son() {
    this.age = '2222'
  }

  Son.prototype = new Father();
  var s = new Son();
  console.log(s.age);
  console.log(s.name);
  s.giveMoney()
  
  console.log(Father.prototype.__proto__); //Object[[proto]]
  console.log(Object.prototype.__proto__);   //null

2. 继承中属性的共享问题

  1. 对象.方法()
  2. new构造函数()
  3. 函数()
    • 直接调用函数,函数中的this是window

2.1 - 函数的借调

call

apply


3. 函数借调的深入理解

    function foo(){
        console.log(this);
        console.log(this.age);
    }
    foo();   //undefined
    foo.call({age:20});    //20
    foo();   //undefined
    function foo(){
        console.log(this[0]);
    }
    foo();   //undefined
    foo.call([10, 20]);  //10
    window.age = 100;
    var obj1 = {
        age : 10,
        speak : function(){
            console.log(this.age);
        }
    }
    obj1.speak();   //10
    var f1 = obj1.speak;
    f1();   //100
    window.age = 10;
    function speak(){
        console.log(this.age);
    }
    var obj1 = {
        age: 20,
        speak : speak
    }
    var obj2 = {
        age : 30,
        speak:obj1.speak
    }

    speak();   //10
    obj1.speak();    //20
    obj2.speak();   //30

3. 借调的应用

  1. typeof
  2. instanceof
  3. Array.isArray
  4. 测试内置类型
    • 借调Object.prototype上的toString
    • 对自定义类型,结果永远是[object Object]
        function foo(){
        }
        var arr = [];
        // console.log(foo.toString());
        console.log(Object.prototype.toString.call(foo));   //[object Function]
        console.log(Object.prototype.toString.call(arr));   //[object Array]
        console.log(Object.prototype.toString.call(3));   //[object Number]
        console.log(Object.prototype.toString.call(null));   //[object Null]
    

4. 作用域链

是变量或函数的查找路径

5. 闭包

理论基础就是作用域链

    function foo(){
        var a = 1;
        function f1(){   //闭包/但是不是很准确
            a++;
        }
        f1();
    }
    foo();
    function foo(){
        var a = 10, b=20;
        function f1(){
            a++;
            b++;
            console.log(a, b);
        }
        return f1;
    }

    // var f = foo();    //在外部拿到了一个闭包
    // f();
    // f();
    var f1 = foo();   //返回一个闭包
    var f2 = foo();     //返回一个新的闭包
    f1();
    f2();

5.1 - 闭包的应用

    //柯里化
    function addFun(m){
        return function f(n){
            return m + n;
        }
        return f;
    }
    var sum = addFun(5);
    console.log(sum(6));   //11
    console.log(sum(7));   //12

6. 三个常用的高阶函数

map //映射

    var arr = [10, 20, 30, 40];
    /*
    内部有内循环,可以对每一个元素做一个映射,然后映射出来的值会组成一个新的数组
     */
    // var mapArr = arr.map(function(e, index, self){
    //     return e * e * e;
    // });
    var mapArr = arr.map(e => e * e);   //拉姆达表达式  Lambda
    console.log(mapArr);    //[1000, 8000, 27000, 64000]
    console.log(arr);    //[10, 20, 30, 40]

filter //过滤

    var arr = [10, 11, 20,33, 30, 40];
    var filterArr = arr.filter(function(e, index, self){
        return !(e % 2);
        // return index % 2;
    })
    /*
    过滤函数,会把回调函数的返回值是true的那些元素给过滤出来,组成一个新的数组
     */
    // var filterArr = arr.filter(e => e % 2);
    console.log(filterArr);
    //数组中的质数,平方之后的到新数组
    var arr = [10, 20, 30, 5, 9, 3, 17, 19];
    var filterArr = arr.filter(function(ele){
        for(var i=2; i<=ele/2; i++){
            return ele % i;
        }
    }).map(e => e * e).sort(function(a,b){
        return b-a;
    });
    console.log(filterArr);   //[361, 289, 81, 25]

reduce //减少, 归纳

    var arr = [10, 20, 30, 5, 9, 3, 17, 19];
    var a = arr.reduce(function(pre, e, index, self){
        console.log(e, index, self);
        return pre * e;
    }, 1);   //初始值
    // var a = arr.reduce((pre, e) => pre * e, 1);
    console.log(a);   //113

7. 回调函数的this丢失问题

    function foo(f){
        f();
    }
    var obj = {
        age : 20,
        f : function(){
            console.log(this.age);
        }
    }
    foo(obj.f);     //undefined
    var age = 30;
    function foo(f){
        f();
    }
    var obj = {
        age : 20,
        f : function(){
            console.log(this.age);
            foo(function(){
                console.log(this.age);
            })
        }
    }
    obj.f();     //20    30;
- 解决:
```
    var age = 30;
    function foo(f){
        f();
    }
    var obj = {
        age : 20,
        f : function(){
            var that = this;  //将动态的this变成变量that/self...
            //闭包的运用
            foo(function(){
                console.log(that.age);
            })
        }
    }
    obj.f();      //20
```

8. 字符串中一些常用的方法和属性

    //生成随机的一个长度为4的字符串可以是数字可以是字母(大小写)
    //验证码
    // var arr = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, "A", "B"];
    var arr = [];
    for(var i=0; i<10; i++){
        arr.push(i + "")
    }
    for(var j=97, z="z".charCodeAt(0); j<=z; j++){
        arr.push(String.fromCharCode(j));
        arr.push(String.fromCharCode(j-32));
    }
    var s = "";
    for(var i=0; i<4; i++){
        s += arr[randomInt(0, arr.length-1)];
    }
    console.log(s);

    function randomInt(m, n){
        return parseInt(Math.random() * (n-m+1) + m);
    }

9. 常用方法

字符串的切片

大小写转换

    var s = "abcca";
    console.log(s.toUpperCase());  //ABCCA
    console.log(s.toLowerCase());  //abcca

去除字符串的首尾空白字符

    var s = "\n       abcca       ddddad     \t";
    console.log("--" + s.trim() + "---");  //--abcca       ddddad---

10. 数学对象

    function toRadian(deg){  //转弧度
        return deg / 180 * Math.PI;
    }
    function toDegree(red){   //转角度
        return red / Math.PI * 180;
    }

11. 时间对象

    //如何知道某年某月一共多少天
    function daysOfMonth(year, month){
        return new Date(year, month, 0).getDate();
    }
    console.log(daysOfMonth(2016, 12));   //31
上一篇下一篇

猜你喜欢

热点阅读