zoukankan      html  css  js  c++  java
  • 原型对象指向可以改变

    原型对象的指向改变

     function Person(age){
        	
        	this.age=10;
        	//在构造函数中的方法
        }
       //人的原型对象的方法
       Person.prototype.eat=function(){
       	console.log("人的吃");
       }
    	//学生的构造函数
    	 function Student(){
    	 	
    	 }
    	 Student.prototype.sayHi=function(){
    	 console.log("学生hi");
    	 };
    	 //学生的原型,指向了一个人的实例对象
    	 Student.prototype=new Person(10);
    	 
    	 var stu=new Student();
    	 
    	 stu.eat();
    	 console.log(stu.age);
    	 stu.sayHi();
    	 
    

    原型对象指向了哪里

       function Person(age){
        	
        	this.age=10;
        	//在构造函数中的方法
        }
       //人的原型对象的方法
       Person.prototype.eat=function(){
       	console.log("人的吃");
       }
       var per =new Person(10);
    	 //原型对象指向了哪里
    	 console.log(per.__proto__==Person.prototype);
    	 console.log(per.__proto__.__proto__== Person.prototype.__proto__);
    	 console.log(Person.prototype.__proto__==Object.prototype);
    	 console.log(Object.prototype.__proto__);
    

    如果原型对象的指向改变了,要先去添加方法

       //如果原型指向改变了,那么就应该在原型改变之后添加原型方法
       
       function Person(age){
       	this.age=age;
       }
       //指向改变了,
       //先添加原型的方法
       Person.prototype.eat=function(){
       	console.log("人在吃");
       }
       //学生构造函数
       function Student(sex){
       	this.sex=sex;
       }
       //学生的原型中添加方法---先在原型中添加
       
       //改变了原型对象的指向
       Student.prototype.sayHi=function(){
       	console.log("你好啊");
       }
       
       Student.prototype=new Person(10);
       
       
       var stu=new Person();
       
       stu.eat();
       stu.sayHi();
    

    实例对象和原型对象重命名的问题

    //原型对象实例对象
    function Person(age,sex){
    	this.age=age;
    	this.sex=sex;
    }
    //
    Person.prototype.sex="女";
    var per=new Person(10,"男");
    

      

     

  • 相关阅读:
    Docker的使用
    单元测试框架--Mocha
    Typescript-规范
    Docker Hello World
    node项目的基本构建流程或者打开一个node项目的流程
    node.js安装及初用
    windows系统安装MongoDB
    微信小程序实现滚动视频自动播放(未优化)
    js判断一个字符串中出现次数最多的字符及次数
    vue相关知识点及面试
  • 原文地址:https://www.cnblogs.com/liushisaonian/p/9459609.html
Copyright © 2011-2022 走看看