Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/376.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
JavaScript对象_Javascript_Object - Fatal编程技术网

JavaScript对象

JavaScript对象,javascript,object,Javascript,Object,我想创建几个特定函数的局部对象,但我希望另一个函数充当助手。helper函数的目的是减少重复代码的数量(随着脚本的进行,最终将创建许多对象) 我将我的脚本“浓缩”下来,确保它工作正常,然后将其复制/粘贴到下面。名为'foo'的函数可以工作(jQuery/ajax调用中没有显示一个php文件)。但是,我希望ajax调用在getNode函数(helper函数)中,最好是在getNode中设置临时对象。我的想法是可以从getNode向foo传递一个临时对象 <script type="text/

我想创建几个特定函数的局部对象,但我希望另一个函数充当助手。helper函数的目的是减少重复代码的数量(随着脚本的进行,最终将创建许多对象)

我将我的脚本“浓缩”下来,确保它工作正常,然后将其复制/粘贴到下面。名为'foo'的函数可以工作(jQuery/ajax调用中没有显示一个php文件)。但是,我希望ajax调用在getNode函数(helper函数)中,最好是在getNode中设置临时对象。我的想法是可以从getNode向foo传递一个临时对象

<script type="text/javascript"> 
    function Node() {
        this.nodeId;
        this.type;  //can be used for diferent shapes, color cobinations, etc.
        this.text;
    }

    function getNode(id){
        $.post("updateDB.php", send, function(theResponse){ 
            var responseArray = theResponse.split(',');
            ....?
    }

    function foo(id){
        var centerNode = new Node();
        var send = 'id=' + id + '&action=getNode';
        $.post("updateDB.php", send, function(theResponse){ 
            var responseArray = theResponse.split(',');
            centerNode.nodeId = responseArray[0];
            centerNode.type = responseArray[1];
            centerNode.text = responseArray[2];         
            alert('nodeId' + centerNode.nodeId);
            alert('node type' + centerNode.type);
            alert('node text' + centerNode.text);
        });                 
    }
</script>
</head>
<body>
 <a onclick="foo(5)">test</a>
</body>

功能节点(){
这是nodeId;
this.type;//可用于不同的形状、颜色组合等。
本文;
}
函数getNode(id){
$.post(“updateDB.php”),发送,函数(响应){
var responseArray=响应拆分(',');
....?
}
函数foo(id){
var centerNode=新节点();
var send='id='+id+'&action=getNode';
$.post(“updateDB.php”),发送,函数(响应){
var responseArray=响应拆分(',');
centerNode.nodeId=responseArray[0];
centerNode.type=responseArray[1];
centerNode.text=responseArray[2];
警报('nodeId'+centerNode.nodeId);
警报('节点类型'+centerNode.type);
警报(“节点文本”+centerNode.text);
});                 
}
测试

一种方法是提供对
getNode
的回调:

function getNode(id, callback){
    $.post("updateDB.php", send, function(theResponse){ 
        var responseArray = theResponse.split(',');
        var node = new Node();
        node.nodeId = responseArray[0];
        node.type = responseArray[1];
        node.text = responseArray[2];         
        callback(node);
    });
}
编辑:“如何设置
foo
?”

因此,在本例中,
foo
是通过获取节点对象并通知其数据来响应单击事件。这是我的理解。如果
getNode
的定义如上所述:

function foo(id) {
  getNode(id, function(node) {
    alert('nodeId' + node.nodeId);
    alert('node type' + node.type);
    alert('node text' + node.text);
  }
});

我们将您的问题归结为要点(我希望局部变量在单独的函数中可用):

  • 可以将对象传递给helper函数
  • 您可以创建一个类,将两个函数都放在其中,并使函数局部成为该类的全局函数
  • 这是我目前唯一能想到的方法

    JS类示例:

    function ClassName(vars){
        //This is used to init the class and is called when you use var myClass = new ClassName();
        //example: this.varName = this.functionName();
        //Instead of using prototype (will see latter in this example) you could just define the function/vars in here like (which would make them private to this class):
        //this.functionName = function(vars){};
    }
    
    ClassName.prototype.functionName = function(vars){}  //Out hear makes these var/functions publicly accessible
    ClassName.prototype.varName = 5;
    
    然后使用它:

    var myClass = new ClassName(vars);
    myClass.functionName(vars);
    

    因此,将您的局部变量和助手函数放入构造函数中,使它们成为私有函数,并将您的其他函数作为原型,使其成为可公开访问的函数。

    谢谢Chris。我对JavaScript相当陌生,所以我只是在谷歌上搜索“回调”,看看如何使用它。“foo”需要如何设置?回调是最棒的/tejavascript的错误功能。事实上,您需要它们,因为语言是异步的。在您调用
    foo
    时的原始示例中,没有
    响应
    数据。这些数据尚未由ajax后回调填充。在我的
    getNode
    函数版本中,我将ajax回调链接到函数中e> foo回调。这有点难以描述,所以我希望这是清楚的。