无论如何有实例共享相同的功能,但同时有私人变量? [英] Is there anyway to have instances share the same function yet at the same time have private variables?

查看:90
本文介绍了无论如何有实例共享相同的功能,但同时有私人变量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这段代码:

  var Human = function(name){
this._name =名称;
};
Human.prototype.Shout = function(){
alert(this._name);
};

var tom = new Human(tom);
var john = new Human(john);
alert(tom.Shout === john.Shout);

现在._name不是private。我想让._name为private,但同时我不希望为每个Human的实例创建额外的函数(换句话说,tom.Shout必须是=== to john.Shout),因为为其创建了额外的函数每个实例都很好..不必要的(可以忽略 - 我们可以在另一个线程上进行讨论)

我的结论是我试图实现的是私人的,同时有 tom.Shout === john.Shout )是不可能的。



但我只想在得出任何结论之前做到200%肯定。



(只要满足要求,我欢迎任何黑客行为,即不为每个实例创建附加功能)

如果我们必须创建额外的函数来进行范围界定,那么这个数字应该是一个固定的数字,并且这个数字不应该随着Human的每个额外实例而增加。

解决方案

更新



您在寻找 @name 这是一个实例变量。祈祷它在es.next,但我们还没有它。可能在两年内。



如果您关心干净的API,那么这里是您的解决方案:

  function Class(foo){
Class.priv(this).foo = foo;


Class.priv =(function(){
var cache = [],
uid = 1;

返回函数(obj){
if(!this .__ id){
this .__ id = uid;
cache [uid ++] = {};
}
return cache [ this .__ id];
};

}());

Class.prototype.bar = function(){
console.log(Class.priv(this).foo);
}

将所有数据作为构造函数的函数存储在缓存中。没有数据混乱的对象。



原始



但没有例如private。

您只需在函数内部创建一个局部变量即可。



构造函数

  var Human = function(name){
//局部变量。
var _name = name;



$ b

有一个局部变量通过定义为本地 strong>在构造函数外部不可用。



这意味着您无法像外部代码那样在原型中访问它。



然而你可以做的是让它只读使用ES5

  var Human = function(name){
Object.defineProperty(this,name,{value:name});

$ / code>

如果你真的能够实现你的要求,你会取得巨大的突破在js。我试图在很长时间内做到这一点。



另一种模式是:

  var Human = function(name){
this.name = name;

return {
Shout:this.Shout.bind(this)
};
}

Human.prototype.Shout = function(){
console.log(this.name);
}

调用 .bind code>并为每个实例创建一个新对象。


I have this piece of code:

var Human=function(name){
  this._name=name;
};
Human.prototype.Shout=function(){
  alert(this._name);
};

var tom=new Human("tom");
var john=new Human("john");
alert(tom.Shout===john.Shout);

Right now ._name is not "private". I want to make ._name "private", but at the same time i do not wish to create additional functions for each instance of Human (in other words tom.Shout Must be === to john.Shout) because creating additional functions for each instance is just well.. unnecessary (ok offtopic - we can debate this on another thread)

My conclusion is that what I'm trying to achieve (having ._name be "private" and at the same time having tom.Shout===john.Shout) is impossible.

But I just want to be 200% sure before jumping into any conclusions.

(I welcome any hacks as long as the requirements are met, i.e no creating of additional functions for each instance)

If we have to create additional functions to do scoping that's fine but that number should be a fixed number and that number should not increase with each additional instance of Human.

解决方案

Update

Your looking for @name which is an instance variable. Pray it's in es.next, but we don't have it yet. Maybe in two years.

If you care about a clean API then here is your solution:

function Class(foo) {
    Class.priv(this).foo = foo;
}

Class.priv = (function() {
    var cache = [],
        uid = 1;

    return function(obj) {
        if (!this.__id) {
            this.__id = uid;
            cache[uid++] = {};
        }
        return cache[this.__id];
    };

}());

Class.prototype.bar = function() {
    console.log(Class.priv(this).foo);
}

Store all the data in a cache as a function of the constructor. No data is cluttered on the object.

Original

However there is no such thing as "private".

All you can do is create a local variable inside a function.

The constructor function

var Human = function(name) {
    // local variable.
    var _name = name;
}

Has a local variable that by very definition of being local is not usable outside of the constructor function.

This means that you cannot access it in external code like the prototype.

What you can do however is make it read only using ES5

var Human = function(name) {
    Object.defineProperty(this, "name", { value: name });
}

If you can truly achieve what your asking, you'd make a huge breakthrough in js. I've attempted to do just that for many hours.

A different pattern would be :

var Human = function(name) {
   this.name = name;

   return {
       Shout: this.Shout.bind(this)
   };
}

Human.prototype.Shout = function() {
   console.log(this.name);
}

This has the overhead of calling .bind and creating a new object for every instance though.

这篇关于无论如何有实例共享相同的功能,但同时有私人变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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