没有办法在Javascript数组定义变量懒干将? [英] Any way to define getters for lazy variables in Javascript arrays?

查看:97
本文介绍了没有办法在Javascript数组定义变量懒干将?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图将元素添加到一个数组,是懒惰的评估。这意味着,对它们的值将不会被计算或已知,直到它们被访问。这就像一个<一个href=\"http://stackoverflow.com/questions/2703304/how-do-i-compute-a-variable-in-javascript-if-and-only-if-it-is-used\">$p$pvious我提出的问题但对象。

I'm trying to add elements to an array that are lazy-evaluated. This means that the value for them will not be calculated or known until they are accessed. This is like a previous question I asked but for objects.

我最终什么事做的对象是

What I ended up doing for objects was

Object.prototype.lazy = function(var_name, value_function) {
  this.__defineGetter__(var_name, function() {
    var saved_value = value_function();
    this.__defineGetter__(var_name, function() {
      return saved_value;
    });
    return saved_value;
  });
}

lazy('exampleField', function() {
  // the code that returns the value I want
});

但我还没有想出了一个办法做到这一点真正的阵列。数组没有这样的制定者。你可以推功能数组,但你必须把它作为调用一个函数它返回你真正想要的对象。我在做什么,现在是我创建的群组我当作一个数组的对象。

But I haven't figured out a way to do it for real Arrays. Arrays don't have setters like that. You could push a function to an array, but you'd have to call it as a function for it to return the object you really want. What I'm doing right now is I created an object that I treat as an array.

Object.prototype.lazy_push = function(value_function) {
  if(!this.length)
    this.length = 0;
  this.lazy(this.length++, value_function);
}

所以,我想知道是什么,有没有办法做到这一点,同时还在做它的阵列上,而不是假的阵列?

So what I want to know is, is there a way to do this while still doing it on an array and not a fake array?

更新:下面的功能仅当value_function返回原始数据类型

UPDATE: The following function works only if the value_function returns a primitive data type.

Array.prototype.lazy_push = function(value_function) {
  var a = this,
  i = this.length;
  this.push({
    toString: function() {
      return a[i] = value_function();
    }
  });
}

如果您尝试推说,在它属性的对象,你将不能够直到你直接访问对象访问属性。这不会发生的制定者,这就是为什么我想要某种JavaScript设置语法。现在我将使用假的阵列,这是足以让我在做什么。

If you try to push say an object that has properties in it, you won't be able to access the properties until you access the object directly. This doesn't happen with setters, that's why I want some kind of setting syntax for Javascript. For now I will be using the fake array, which is enough for what I'm doing.

推荐答案

好吧,除非我误解你的问题,我相信我已经找到了一个简单的解决方案,它不需要所谓的lazy_push功能。

Ok, unless I misunderstood your question, I believe I have found a simple solution which does not require a so-called "lazy_push" function.

继从我的previous答案的方法,创建一个MYARRAY类:

Following the method from my previous answer, you create a MyArray Class:

function MyArray(){
     this.object = [];
 }

MyArray.prototype.push = function(what){
     this.object.push(what);
}

现在的重要组成部分,是getter函数,我们将创建一个getIdx()函数来获取值从数组中。然后,该函数使用typeof运算运营商以确定是否返回的值是一个函数。如果是,则返回从函数返回的值,如果不是返回原始值。

Now the important part is the getter function, we will create a getIdx() function to grab the value out of the array. The function then uses the 'typeof' operator to determine if the returned value is a function. If it is, return the value returned from the function, if not return the original value.

code更有意义:

MyArray.prototype.getIdx = function(which){
     if(typeof this.object[which] == 'function'){
         alert("a function");
         //NOTICE THE '()' AT THE END OF THE NEXT LINE!!!
         return this.object[which]();
     }
     return this.object[which];
 }

我希望如果我没有完全回答你的问题,你可以从这里找到答案。

Hopefully if I didn't completely answer your question you can figure it out from here.

&LT; ---------我原来的职位------------->

<--------- My original post ------------->

不是一个真正的答案,但有几个要点。

Not really an answer, but a few important points.


  1. 有在Javascript中没有真正的数组,数组只是一个扩展的对象(如在JS一切)

  1. There are no real arrays in Javascript, an Array is just an extended Object (as is everything in JS)

您应该尽可能不增加原型功能,在JS原生对象,你可能会意外地覆盖现有属性,或者创建混乱错误的路线。例如,加入到对象的原型是要添加到每一个对象在JS(这就是一切),你需要绝对确保你想在每一个JS型拥有该属性。这仅仅是危险的,因为如果你不小心覆盖实际阵列()或对象()函数,你会在浏览器中的JavaScript突破,期间,页面的刷新将不会解决它。

You should ideally never add prototype functions to the native objects in JS, you might accidentally overwrite an existing property, or create confusing errors down the line. For instance, adding to the prototype of Object is going to add to every single Object in JS (which is everything), you need to make absolutely sure that you want every type in JS to have that property. This is just dangerous because if you accidentally overwrite the actual Array() or Object() functions, you will break javascript in the browser, period, a refresh of the page won't fix it.

而不是增加你正在修改的对象的原型,创建继承它的新对象。例如,如果你想扩展Array类:

Rather than adding to the prototype of the Object you are modifying, create a new Object that extends it. For instance if you want to extend the Array class:

//create the constructor, 
//with an object variable that holds the object you wish to extend
function MyArray(){
     this.object = [];
}


//create functions which reference the native functions of the object
MyArray.prototype.push = function(what){
     this.object.push(what);
}


//Etc... Etc....  Etc.....


它不一定好玩写所有原生对象功能的访问方法,但它一直JavaScript引擎是安全的。

Its not necessarily fun writing all the accessor methods for native Object functions, but it keeps the Javascript engine safe.

这篇关于没有办法在Javascript数组定义变量懒干将?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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