javascript - 在 jQuery 中使用字符串调用嵌套对象函数

标签 javascript jquery object

我有这段代码,它本身运行得很好:

var Person = {
    getName: function() {
        alert("Johnny");
    }
};

var myContext = "Person";
var theCodeToExecute = myContext + ".getName()";
var theFunctionItself = new Function(theCodeToExecute);
theFunctionItself(); 

但是当我将它放入 jQuery 中时,它停止工作。

$(document).ready(function() {
    //Where the code should be.
});

我知道已经回答了非常相似的问题,但没有一个解决了这个确切的问题。

编辑:

谢谢大家的回答。我想我太缩小了问题的范围,以至于我的意图变得非常不清楚。这是我想要实现的目标的更清晰版本:

var Road = {
       House: function(color, size, neighbor){
          this.color = color;
          this.size = size;
          this.neighbor = neighbor;
       }
};

Road.House.prototype.getSize = function() {
    alert(this.size);
};

Road.House.prototype.getNeighborSize = function() {
    var theNeighbor = this.neighbor;
    var codeToExecute = theNeighbor + ".getSize()";
    var tmpFunc = new Function(codeToExecute);
    tmpFunc();    //this only works when outside of jQuery.    
};

var house1 = new Road.House("blue", "small", "house2");

var house2 = new Road.House("red", "huge", "house3");

house1.getNeighborSize(); //this successfully returns "huge" when outside of jQuery.

同样,它本身工作得很好,但在 jQuery 中不起作用,我需要它在 jQuery 中工作,因为我的函数的最终版本将使用大量 jQuery 代码。再次感谢!

最后编辑:

感谢菲利克斯的出色帮助。似乎还有最后一个问题。如果邻居是在我查询的房子之前声明的,我只能获取邻居的大小。

var house1 = new Road.House("red", "big", house2);
var house2 = new Road.House("blue", "huge", house3);
var house4 = new Road.House("blue", "small", house4);
var house3 = new Road.House("blue", "little", house1);

house1.getNeighborSize(); //this doesn't work.

house3.getNeighborSize(); //this works

再次感谢!!

最佳答案

当您使用new Function创建函数时,它将在全局范围中创建。 IE。它无法访问您在 ready 回调内部创建的变量。

// global scope

$(document).ready(function() {

   // ready callback scope    

});

引用 MDN 中的黄色大框这里:

Note: Functions created with the Function constructor do not create closures to their creation contexts; they always are created in the global scope. When running them, they will only be able to access their own local variables and global ones, not the ones from the scope in which the Function constructor was called. This is different from using eval with code for a function expression.

解决方案:

  1. 在全局范围内创建Person
  2. 使用eval而不是new Function

最棒的是:

  • 不要动态评估代码。您可以直接编写 Person.getName()
  • 即使您认为必须评估代码,您也可能不会,但如果您不解释您真正想要实现的目标,我们无法帮助您找到替代方案。

    <小时/>

    嗯,解决你的问题的实际方法不是尝试动态引用变量,而是将对邻居的引用传递给构造函数:

    var house2 = new Road.House("red", "huge");
    var house1 = new Road.House("red", "huge", house2);
    

    那么该函数将如下所示

    Road.House.prototype.getNeighborSize = function() {
        if (this.neighbor) {
            this.neighbor.getSize();
        } 
    };
    

    如果在构造时无法将引用传递给邻居,则可以创建一个 setter:

    Road.House.prototype.setNeighbor = function(neighbor) {
        this.neighbor = neighbor;
    };
    

    关于javascript - 在 jQuery 中使用字符串调用嵌套对象函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27463653/

    相关文章:

    html - 自定义样式的单选按钮?

    java - 让类(class)相互交换信息的最佳方式?

    javascript - 获取值不相等的随机数组元素

    javascript - 似乎无法在不破坏父模式的情况下关闭此模式?

    jquery - 使用 Javascript 更改字段值时,Safari 保持占位符可见

    javascript - 如何模糊除列表项之外的整个主体?

    javascript - 过滤拒绝的 promise

    javascript - 如何获取除HTML之外的整个文档的文本内容?

    Python对象显示为map(dict)但无法通过属性或__getitem__调用?

    java - 将对象添加到数组时动态增加数组长度