前端 原型对象中this的认识

今天我看了一篇博文,博文是关于原型对象介绍的。我把代码粘贴出来如下:

    function SuperType(){}  
            
     SuperType.prototype.name = 'Sam';//在SuperType.prototype指向的对象上增加了name属性  
          
     //在SuperType.prototype指向的对象上增加了sayName方法  
     SuperType.prototype.sayName = function() {  
              document.write(this.name);  
     };  
     SuperType.prototype.setName = function(name) {  
               //修改这行,结果会不同
              SuperType.prototype.name = name;  
     };  
          
     var instance1 = new SuperType();  
     var instance2 = new SuperType();  
          
     instance1.sayName();//Sam  
     instance2.sayName();//Sam  
          
     instance1.setName('Luce');  
           
     instance1.sayName();//Luce  
     instance2.sayName();//Luce  

如果把第十行的代码改为:

this.name  = name;

在进行调用出现的结果就会不同:

var instance1 = new SuperType();

var instance2 = new SuperType(); 

instance1.sayName();//Sam  
instance2.sayName();//Sam
instance1.setName('Luce'); 
instance1.sayName();//Luce

输出的结果不在是Luce

instance2.sayName();//Sam

为什么??把第十行改为了this.name = name 之后输出的结果不在是Luce而是Sam

关键问题在于this指向的是哪里??

原型中的this不是指的原型对象,而是调用对象。

怎样来理解这一句话???

下面来看一下一个简单的例子

function animal() {
 }
 animal.prototype.name = '张三';
 animal.prototype.sayhello = function () {
   console.log(this.name);
 };

 animal.prototype.setName = function (name) {
   this.name = name;
 };

 var dog = new animal();
 var cat = new animal();
 dog.sayhello();   //张三

cat.sayhello();   //张三
  1. console.log(animal.hasOwnProperty("name")); //true
 dog.setName('xiaoshu');
  1. console.log(animal.hasOwnProperty("name")); //true
dog.sayhello();    // xiaoshu
cat.sayhello();     //张三

为什么不是我想的结果???我认为1.输出的是false,2.输出的是true

为什么,开始找问题在哪里??????????

  1. console.log(animal.hasOwnProperty("name")); //true这个是true ,照道理不应该是false吗??
    我自己把animal看了一下,自带一个name属性
    在这里插入图片描述
    难怪 console.log(animal.hasOwnProperty("name")); //truetrue

修改:
那我把name改为name1

function animal() {
 }
 animal.prototype.name1 = '张三';
 animal.prototype.sayhello = function () {
   console.log(this.name1);
 };

 animal.prototype.setName = function (name) {
   this.name1 = name;
 };

 var dog = new animal();
 var cat = new animal();
 dog.sayhello();   //张三

cat.sayhello();   //张三
  1. console.log(animal.hasOwnProperty("name1")); //false
 dog.setName('xiaoshu');
  1. console.log(animal.hasOwnProperty("name1")); //false
dog.sayhello();    // xiaoshu
cat.sayhello();     //张三

为什么还不是自己想要的结果????抓狂!!! 继续改

function animal() {
 }
 animal.prototype.name1 = '张三';
 animal.prototype.sayhello = function () {
   console.log(this.name1);
 };

 animal.prototype.setName = function (name) {
   this.name1 = name;
 };

 var dog = new animal();
 var cat = new animal();
 dog.sayhello();   //张三

cat.sayhello();   //张三
  1. console.log(dog.hasOwnProperty("name1")); //false

  2. console.log(cat.hasOwnProperty("name1")); //false

 dog.setName('xiaoshu');
  1. console.log(dog.hasOwnProperty("name1")); //true

  2. console.log(cat.hasOwnProperty("name1")); //false

dog.sayhello();    // xiaoshu
cat.sayhello();     //张三

终于看到了自己想要的结果!!!!!
dogcat是调用的对象
在执行dog.setName('xiaoshu');这句之后,setName中的this指向的就是dogdog这个调用对象中就多了一个name1这个属性。而这个name1属性不在animal函数中,只存在于dog这个实例对象中,归dog私自拥有,不是共享的。

这说明这个this是指向调用对象,而不是指向原型的。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值