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