小编Fra*_*ois的帖子

原型上的Javascript私有成员

好吧,我试图弄清楚这有可能以任何方式.这是代码:

a=function(text)
{
   var b=text;
   if (!arguments.callee.prototype.get)
      arguments.callee.prototype.get=function()
    {
         return b;
    }
    else
      alert('already created!');
}

var c=new a("test");  // creates prototype instance of getter
var d=new a("ojoj");  // alerts already created
alert(c.get())        // alerts test 
alert(d.get())        // alerts test from context of creating prototype function :(
Run Code Online (Sandbox Code Playgroud)

如你所见,我试图创建原型getter.为了什么?好吧,如果你写这样的东西:

a=function(text)
{
    var b=text;
    this.getText=function(){ return b}
}
Run Code Online (Sandbox Code Playgroud)

......一切都应该没问题......但实际上每次创建对象时 - 我都会创建使用内存的getText函数.我想在记忆中有一个原型功能可以做同样的事情...任何想法?

编辑:

我试过Christoph给出的解决方案,它似乎是目前唯一已知的解决方案.它需要记住id信息以从上下文中检索值,但是整个想法对我来说很好:) Id只是要记住的一件事,其他一切都可以在内存中存储一​​次.实际上,您可以通过这种方式存储许多私有成员,并且只能使用一个id.实际上这让我很满意:)(除非有人有更好的主意).

someFunc = function()
{
  var store = new Array();
  var guid=0;
  var someFunc = function(text)
  {
    this.__guid=guid;
    store[guid++]=text; …
Run Code Online (Sandbox Code Playgroud)

javascript prototype private

17
推荐指数
1
解决办法
2万
查看次数

在使用显示模块模式时,如何查看eclipse中的轮廓?

我目前正在重构一些我们拥有的Javascript代码,除此之外我还改变了它以利用揭示模块模式.代码看起来更整洁,它工作正常但我在大纲视图中看不到这些功能.我将顶级命名空间var视为var,但您无法展开它以查看其中的函数.

让我们说代码看起来像这样:

function myFunc1() {}
function myFunc2() {}
Run Code Online (Sandbox Code Playgroud)

在这种情况下,您可以在大纲视图中看到这两个函数.但是如果你改成它:

var myNamespace = function()
{
  function myFunc1() {}
  function myFunc2() {}

  return {
    name: "myNamespace",
    myFunc1: myFunc1,
    myFunc2: myFunc2
  }
}();
Run Code Online (Sandbox Code Playgroud)

然后大纲视图只显示myNamespace var.我试过看但却找不到能够正确显示层次结构的视图.有没有人知道一种方法来查看这个或者是eclipse不能这样做的情况?

javascript eclipse jsdoc revealing-module-pattern

17
推荐指数
2
解决办法
7093
查看次数

boost :: program_options:当它属于命名空间时,如何声明和验证我自己的选项类型?

使用boost :: program_options,在命名空间内声明时,我无法获得自己的选项类型进行编译.但是在命名空间之外它编译并且工作正常:

#include <boost/program_options.hpp>
using namespace boost;
using namespace boost::program_options;

struct my_type1 {
    my_type1(int nn) : n(nn) {}
    int n;
};
namespace nm  {
    struct my_type2 {
        my_type2(int nn) : n(nn) {}
        int n;
    };
}

void validate(boost::any& v,
              const std::vector<std::string>& values,
              my_type1*, int)  {
    const std::string& s = validators::get_single_string(values);
    v = any(my_type1(lexical_cast<int>(s)));
}
void validate(boost::any& v,
              const std::vector<std::string>& values,
              nm::my_type2*, int)  {
    const std::string& s = validators::get_single_string(values);
    v = any(nm::my_type2(lexical_cast<int>(s)));
}

int main()  {
    options_description desc("options");
    desc.add_options()
        ("m1", …
Run Code Online (Sandbox Code Playgroud)

validation boost namespaces boost-program-options

3
推荐指数
1
解决办法
2286
查看次数

javascript中对象的数学操作

我试图在这样的对象中操纵属性:

for (property in posts.data) {
    property = property+1+",";
    output += property;
}
document.write(output);
Run Code Online (Sandbox Code Playgroud)

属性是数字:0,1,2等.

在这种情况下,我想得到结果1,2,3.但相反,我得到01,11,21等...它似乎将属性视为文本字符串而不是数字.为什么?我能做些什么呢?

javascript

3
推荐指数
1
解决办法
108
查看次数