this.bla与Object.prototype.bla有什么区别 [英] What's the difference between this.bla to Object.prototype.bla

查看:100
本文介绍了this.bla与Object.prototype.bla有什么区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

 (function(global){
function Bar(foo){
this.foo = foo;
return this;
}

Bar.prototype.getFoo = function(){
return this.foo;
};

Bar.prototype.setFoo = function(val){
return(this.foo = val);
};
})(这个);

创建函数如 setFoo with prototype ,只是这样做:

 函数Bar foo){
this.getFoo = function(){
return this.foo;
};
}

我知道原型是什么,它是什么意思,我无法确定为什么有些人使用原型来分配函数,因为如果我将它们赋值给它们,每次创建Bar的新实例时它们都可用。

解决方案

快速回答=功能分享+内存占用小



当您使用 prototype.functionName 所有实例共享相同的函数(在内存中只有一个副本),但是如果在构造函数中使用 this.functionName 自己的同一个函数的副本(在内存中存在多次)。

使用 prototype $ b

$ c $有两个含义:
$ b $ ol

  • 内存占用 - 如前所述
  • 随后的函数更改原型反映在所有现有的(当然还有将来的)实例上 - 当有一个人时,很少会出现这种情况ld喜欢这样做,但它可以使用



  • 高级 - 您可以同时使用



    在这种情况下,您可以同时使用 local 副本优先于原型,这意味着您可以执行下列操作:

     函数Crazy(name)
    {
    this.name = name;
    this.callMe = function(){
    returnWhere are you+ this.name;
    };
    }

    Crazy.prototype.callMe = function(){
    return this.name +come here;
    };

    var inst = new Crazy(Robert);
    inst.callMe(); //你在哪里罗伯特
    删除inst.callMe;
    inst.callMe(); //罗伯特来这里


    Let's say I have this code:

    (function(global) {
        function Bar(foo) {
            this.foo = foo;
            return this;
        }
    
        Bar.prototype.getFoo = function() {
            return this.foo;
        };
    
        Bar.prototype.setFoo = function(val) {
            return (this.foo = val);
        };
    })(this);
    

    What is the difference between creating functions like setFoo with prototype and just doing it like this:

    function Bar(foo) {
        this.getFoo = function() {
            return this.foo;
        };
    }
    

    I know what prototype is and what it means, I just can't figure out, why some people assign functions with prototype, because if I assign them with this, they will be available also every time I create a new instance of Bar.

    解决方案

    The quick answer = function sharing + smaller memory footprint

    When you're using prototype.functionName all instances share the same function (only one copy in memory), but if you use this.functionName in your constructor each instance has its own copy of the same function (exists multiple times in memory).

    Using prototype has two implications:

    1. Memory footprint - as mentioned
    2. Subsequent function change on the prototype is reflected on all existing (and future of course) instances - there are rare occasions when one would like to do this, but it's there any it can be used

    Advanced - you can have both

    You can also have both in which case the local copy has precedence over the prototype which means that you could do stuff like this:

    function Crazy(name)
    {
        this.name = name;
        this.callMe = function() {
            return "Where are you " + this.name;
        };
    }
    
    Crazy.prototype.callMe = function() {
        return this.name + " come here";
    };
    
    var inst = new Crazy("Robert");
    inst.callMe(); // "Where are you Robert"
    delete inst.callMe;
    inst.callMe(); // "Robert come here"
    

    这篇关于this.bla与Object.prototype.bla有什么区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    相关文章
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆