call apply 原型 原型链

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
</head>
<body>
    <script type="text/javascript">
    function A(name){
        this.name=name;
    }
    function B(age){
        this.age=age;
    }
    function C(sex,name,age){
        this.sex=sex;
       A.call(this,name);//apply(this,[name])
       B.call(this,age)
    }  
    var c=new C('male','wnag',17); 
     </script>
</body>
</html>
//call需要把实参按照形参的个数传递进去
//apply需要传一个arguments的
//改变this指向 ,传参不同

 原型

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
</head>
<body>
    <script type="text/javascript">
    //Person.prototype---原型
    //Person.prototype={}---祖先
    Person.prototype.name="like"; //公有祖先
    Person.prototype.say=function (){
        console.log('love');
    }  
    function Person(age,sex){
      this.age=age;
      this.sex=sex;
    }
    var person= new  Person(18,'male');
    var person1=new Person(40,'femal');    
       </script>
</body>
</html>
//1.函数对象有_proto_和prototype属性
 2.非函数对象只有_proto_属性
 3.prototype中有_proto_属性,是Object构造出来的
 4.函数对象_proto_指向它的创建者及Function构造函数
 5.Function构造函数_proto_指向它自己
 6.Object对象的prototype中的_proto_是null

原型链

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
</head>
<body>
    <script type="text/javascript">
    Grand.prototype.name='wang';
    function Grand(){

    }
    var grand=new Grand();
    Father.prototype=grand;
    function Father(){
        this.name='xu';
    }
    var father=new Father();
    Person.prototype=father;
    function Person(){
        this.hobbit='woshiwudi';
    }
    var person =new Person();
    </script>
</body>
</html>

var son={
    name:'lang';
}
var person=Object.create(son);
Object.create(null)没有原型

猜你喜欢

转载自www.cnblogs.com/wxy0715/p/12442280.html