如何创建Prototype函数可访问的私有变量? [英] How to create private variable accessible to Prototype function?

查看:427
本文介绍了如何创建Prototype函数可访问的私有变量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在努力深入了解原型继承和类创建(我知道,还有其他方法,但出于此目的,我正在尝试掌握原型。)我的问题是:使用以下代码例如,有没有办法在 Fruit 中创建私有变量,该变量不会随函数返回,但仍然可以访问原型函数 genus 批量

I'm trying to get a deeper hold on prototypal inheritance and class creation (I know, there are other ways, but for the purpose of this I'm trying to grasp prototypes.) My question is: Using the following code example, is there a way to create private variables inside of Tree and Fruit that will not be returned with the function, but is still accessible to the prototype functions genus and bulk?

var Tree = function ( name, size ) { 
    this.name = name;
    this.size = size;
};

Tree.prototype.genus = function(){
    return ((typeof this.name !== 'undefined') ? this.name : 'Hybridicus Maximus');
};
Tree.prototype.bulk = function(){
    return ((typeof this.size !== 'undefined') ? this.size : '8') + ' ft';
};


var Fruit = function( name, size ) { 
    this.name = name;
    this.size = size;
};

Fruit.prototype = new Tree();
// Fruit.prototype = Tree.prototype; -- I know this can be used, too.

Fruit.prototype.bulk =  function(){
    return ((typeof this.size !== 'undefined') ? Math.floor(this.size / 2) : '4') + ' lbs';
};

var pine = new Tree('Pine', 9);
var apple = new Fruit('Apple', 6);

console.log(pine.genus(), pine.bulk()); // Outputs: "Pine 9 ft"
console.log(apple.genus(), apple.bulk()); // Outputs: "Apple 3 lbs"

编辑:我是尝试使用可在原型函数中访问的私有变量替换 this.name this.size 。抱歉缺乏清晰度!

EDIT: I'm trying to replace this.name and this.size with private variables that can be accessed in the prototype functions. Sorry for the lack of clarity!

推荐答案

是的。你可以这样做:

(function() {
  var private = "hi";

  Tree.prototype.genus = function(){
    return ((typeof this.name !== 'undefined') ? this.name : 'Hybridicus Maximus');
  };
  Tree.prototype.bulk = function(){
    return ((typeof this.size !== 'undefined') ? this.size : '8') + ' ft';
  };
})();

现在,这将提供这些函数可以看到的私有变量,但它将是一个私有类变量 - 换句话说,所有实例将共享相同的变量。如果你想要每个实例的一个私有变量,你必须在构造函数(或init方法,或其他)中这样做,这意味着必须在那里创建共享这些私有的方法。 (你当然可以在原型上放置一个函数,它将在构造时创建实例方法。)

Now, that'll provide a private variable that those functions can see, but it'll be a private "class" variable - all instances will share the same variable, in other words. If you want a private variable per instance, you have to do that in the constructor (or "init" method, or whatever), meaning the methods that share those privates would also have to be created there. (You could of course put a function on the prototype that would create the instance methods at construction time.)

edit —你可以做的一件事是使用这样的技术来构建像jQuery的.data()这样的机制,这样你就有了一个类变量作为保存每个实例值的地方。它有点笨重,但它是可行的。

edit — One thing you could do is use a technique like this to build a mechanism like jQuery's ".data()", so that you'd have a class variable that acts as a place to keep per-instance values. It'd be kind-of clunky, but it'd be workable.

这篇关于如何创建Prototype函数可访问的私有变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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