js 进阶语法

2019-04-24  本文已影响0人  不染事非
知识点
原型链
什么是原型链

1.1 由于proto是任何对象都有的属性,而js里万物皆对象,所以会形成以proto链接起来的链条,递归访问proto必须最终到头,并且值为null
当js 引擎查找对象的属性时,先查找对象本身是否存在该属性,如果不存在,会在原型链上查找,但不会查找自身的prototype
1.2 只有函数有prototype,对象是没有的。但是函数也是有proto的,因为函数也是对象。函数的proto指向的是Function.prototype。

原型链.png
instanceof 的原理
126.png
instanceof是判断实例对象的proto和生成该实例的构造函数的prototype是不是引用的同一个地址。是返回true,否返回false。
例子
var F = function() {};
Object.prototype.a = function() {
  console.log("aa");
};
Function.prototype.b = function() {
  console.log("bb");
};
var f = new F();
F.a();  //aa
F.b();  //bb
f.a();   //aa 
f.b();   // 抱错,f.b is not a function

this 的指向(有四种)

第一种 :方法调用 this 指向调用它的对象
var age = 38;
var obj = {
    age: 18,
    getAge: function() {
        console.log(this.age); // age=18
    }
};
obj.getAge(); 
--------------------------------------------------
// 变式:
var fn = obj.getAge;
fn();
第二种:函数调用 里面的this指向window
var age = 38;
var obj = {
    age: 18,
    getAge: function() {
        var fn = function() {
            console.log(this.age);// age=38
        };
        fn();
    }
};
obj.getAge();
第三种:构造函数调用 this绑定到当前创建的对象实例。
var age = 38;
var obj = {
    age: 18
};

var getAge = function() {
    console.log(this.age); //age =18
};
obj.get = getAge;
obj.get(); 
第四种 上下文模式 this 指向谁? this指向的是传入的对象
call 和apply
 var arr = [1,3,4,6,7,555,333,13]
 console.log(Math.max(1,2,3,4,5))
 console.log(Math.max.apply(arr,arr)) //this指向arr
 console.log(Math.max.call(arr,1,3,4,6,7,555,333,13))
 
//伪数组 是一个对象: 访问方式:和数组一样,不能使用数组的方法
function sum(){
     console.log(arguments[2]);
     console.log(Array.prototype.join.call(arguments,"-"))
     arguments.join("-")
     }
   sum("aa","bb","cc")
作用域和预解析

let const 函数作用域 ,{}
预解析(将声明的变量提前,声明的函数提前))只支持es5 ,不支持es6 。

new 关键字的作用
闭包

例子

var counter = (function() {
   //命名空间
 var privateCounter = 0;
 function changeBy(val) {
   privateCounter += val;
 }
 return {
   increment: function() {
     changeBy(1);
   },
   decrement: function() {
     changeBy(-1);
   },
   value: function() {
     return privateCounter;
   }
 };   
})();
继承
es5继承(有三种)
第一种 构造函数继承( 缺点是:原型上的方法或者属性,无法继承)
function Fn(){
    this.name = "zhangsan",
    this.age = 12
    this.eat = function(){
          console.log("eat")
      }
 }
    Fn.prototype.sleep = function(){
          console.log("sleep")
    } // 无法继承
    function F(){
     console.log(this)
     Array.prototype.join.call(obj,'-')  // 上下文模式 call 和 apply
         Array.prototype.join.apply(obj,['-'])
    Fn.call(this)
     }
     var fn = new Fn()
     console.log(fn)
     var f = new F()
    console.log(f.sleep())
第二种 原型继承

缺点是: 共用一个原型对象,导致谁修改原型对象的值,其余对象都会被更改

function Fn(){
        this.name = "zhangsan"
        this.age = 12
        this.color = ["yellow", "pink"]
        this.eat = function(){
                console.log("eat")
                 }
         }
         Fn.prototype.sleep=function(){
            console.log("sleep")
        }
        function F(){}
        F.prototype = new Fn()
       var f = new F()
       var f1 = new F()
       f.color.push("black")
       console.log(f1.color)// color=['yellow','pink','black']
第三种 组合方式
 function Fn(){
            this.name = "zhangsan"
            this.age = 12
            this.color = ["yellow", "pink"]
            this.eat = function(){
                console.log("eat")
            }
        }
        function F(){
             Fn.call(this)  // this指向 Fn()
        }
        F.prototype = Object.create(Fn.prototype)
        // F.prototype = Fn.prototype.constructor === Fn
        F.prototype.constructor = F
        
        var f = new F()
        var f1 = new F()
        f.color.push("black")
        console.log(f1.color) //color =["yellow", "pink"]
        
        function FCC(){}
        console.log(f instanceof FCC)
        console.log(f instanceof Fn)

es6 继承
class People{
            constructor(name,age,skin){
                this.name = name,
                this.age = age
                this.skin = skin
            }
            eat(){
                console.log("eat")
            }
            sleep(){
                console.log("sleep")
            }
            speak(){
                console.log("speak")
            }
        }  
        class Student extends People{
             constructor(name,age,skin,id,classs){
                 super(name,age,skin) 
                 this.id = id
                 this.classs = classs
             }
             study(){
                 console.log("study")
             }
        }
        var stu = new Student("zhangsan",12,"yellow",110,'一年级01班')

上一篇 下一篇

猜你喜欢

热点阅读