由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
Programming版 - ajax小问题
相关主题
Default function template arguments问个问题
static如何作为函数?[合集] C/C++ calling function by name
感觉学scala不学haskell是不行的[合集] matlab函数的返回变量
one more interview questionhelp: matlab integral function
Question: How to store initialize arguments?how to write a function take iterators as parameters?
问一个C++问题:default parameter and overriding/inheritanc (转载)今天很郁闷
question about using Hive parameter (转载)C++ Q 103-105 (转载)
一个面试题目,用C实现C++ online Test 又一题 (转载)
相关话题的讨论汇总
话题: function话题: name话题: deleterow话题: var话题: 作用域
进入Programming版参与讨论
1 (共1页)
N***m
发帖数: 4460
1
我看不出来bug在哪里。。。
我想动态的更新表格 id="tableiv",然后在表格上面添加一些button,
可以删除表格对应的这一行。问题是运行的时候表格不删除第一行,尽管
点击的是第一行:)
for(var i=0;i<10;i++)
{
var row = document.createElement("tr");
var name = "row_"+i;
row.setAttribute("id",name);
var cell = document.createElement("td");
var delButton = document.createElement("input");
delButton.setAttribute("type","button");
delButton.setAttribute("value","delete");
delButton.onclick = function(){deleteRow(name);};
X****r
发帖数: 3557
2
Here:
delButton.onclick = function(){deleteRow(name);};
You use the local variable 'name' in the closure. However,
the value of this variable get overwritten in the next
iteration of the loop.
Instead, use
delButton.onclick = (function(name) {
return function(){deleteRow(name);};
})(name);

【在 N***m 的大作中提到】
: 我看不出来bug在哪里。。。
: 我想动态的更新表格 id="tableiv",然后在表格上面添加一些button,
: 可以删除表格对应的这一行。问题是运行的时候表格不删除第一行,尽管
: 点击的是第一行:)
: for(var i=0;i<10;i++)
: {
: var row = document.createElement("tr");
: var name = "row_"+i;
: row.setAttribute("id",name);
: var cell = document.createElement("td");

N***m
发帖数: 4460
3
大虾真是高山仰止阿。
只是我还是不明白local variable这里为什么不行?
难道不是每次循环name = row_0, row_1 etc...负值给调用的anonymous function?

【在 X****r 的大作中提到】
: Here:
: delButton.onclick = function(){deleteRow(name);};
: You use the local variable 'name' in the closure. However,
: the value of this variable get overwritten in the next
: iteration of the loop.
: Instead, use
: delButton.onclick = (function(name) {
: return function(){deleteRow(name);};
: })(name);

X****r
发帖数: 3557
4
内层的函数是直接存取外层的变量的。当每循环一次,一个新的内层函数被创建的时候,
外层的变量并没有被复制到内层,而是内层有一个隐含的指针,当一个变量名在内层自身
查找不到时就到外层去查找。所以你虽然有十个内层的匿名函数,它们都指向同一个外
层,
自然它们共用同一套外层的变量。
For more information, check out "scope chain" in ECMA 262.

【在 N***m 的大作中提到】
: 大虾真是高山仰止阿。
: 只是我还是不明白local variable这里为什么不行?
: 难道不是每次循环name = row_0, row_1 etc...负值给调用的anonymous function?

N***m
发帖数: 4460
5
多谢:)
我看了十遍你说的话,我大概理解你说的。你意思是说外层变量(程序里面的var name)
虽然在每次循环是改变的,但是对于匿名函数deleteRow(_name)来说,它的参量_name
只是指向var name的“指针”。我其实一直不理解:每次循环var name一直在改变,
按理说每次循环它的地址也应该是改变的?还是匿名函数按照ECMA就是这么工作的?
btw,ECMA 262 只找到一处scope chain,我不知道怎么联系到具体的例子:)

候,
自身

【在 X****r 的大作中提到】
: 内层的函数是直接存取外层的变量的。当每循环一次,一个新的内层函数被创建的时候,
: 外层的变量并没有被复制到内层,而是内层有一个隐含的指针,当一个变量名在内层自身
: 查找不到时就到外层去查找。所以你虽然有十个内层的匿名函数,它们都指向同一个外
: 层,
: 自然它们共用同一套外层的变量。
: For more information, check out "scope chain" in ECMA 262.

X****r
发帖数: 3557
6
Javascript里面所有的变量都有一个作用域(scope)。有两种作用域:全局
(global)和函数(function)。如果一个函数(或者全局)里有var x;
这样的声明(并且不在更内层的函数里),x就作为一个变量加到了当前函数(或
者全局)的作用域里,无论这个声明所在的位置会不会被执行到或者执行多少次。
当遇到内层函数的时候(无论匿名与否都是一样的),就会为其创建一个新的作用
域,并且把这个新的作用域加到当前的作用域链的顶部作为其作用域链。
当程序执行到某处的时候,比如要存取一个叫y的变量,那就会先看当前作用域里
有没有一个y。要是没有的话,就沿着作用域链看下一个作用域里有没有一个y,
以此类推。举个例子:
// x和y被加到全局作用域,因为后面有var x和var y的声明
var x = 1; // 将全局作用域里的x赋值为1
var y; // 什么也没有发生
function f() { // 函数f()里有var x这个声明,x被加到f()的作用域
x = 2; // 先看当前的作用域,有x,所以将f()里的x赋值为2
if (0) {
va

【在 N***m 的大作中提到】
: 多谢:)
: 我看了十遍你说的话,我大概理解你说的。你意思是说外层变量(程序里面的var name)
: 虽然在每次循环是改变的,但是对于匿名函数deleteRow(_name)来说,它的参量_name
: 只是指向var name的“指针”。我其实一直不理解:每次循环var name一直在改变,
: 按理说每次循环它的地址也应该是改变的?还是匿名函数按照ECMA就是这么工作的?
: btw,ECMA 262 只找到一处scope chain,我不知道怎么联系到具体的例子:)
:
: 候,
: 自身

N***m
发帖数: 4460
7
Thank you very much for such a detailed reply. I have little experience
with call back function before.
So I guess the question is actually due to the call back function
"deleteRow(_name)", whose parameter "_name" in this case only points
to the outer scope variable "name". And since the event function is called
back later, "_name" gets updated only after the loop finishes. Initially,
I naively think that once the call back function is attached to a
onclick event, its parameter is assigned to t

【在 X****r 的大作中提到】
: Javascript里面所有的变量都有一个作用域(scope)。有两种作用域:全局
: (global)和函数(function)。如果一个函数(或者全局)里有var x;
: 这样的声明(并且不在更内层的函数里),x就作为一个变量加到了当前函数(或
: 者全局)的作用域里,无论这个声明所在的位置会不会被执行到或者执行多少次。
: 当遇到内层函数的时候(无论匿名与否都是一样的),就会为其创建一个新的作用
: 域,并且把这个新的作用域加到当前的作用域链的顶部作为其作用域链。
: 当程序执行到某处的时候,比如要存取一个叫y的变量,那就会先看当前作用域里
: 有没有一个y。要是没有的话,就沿着作用域链看下一个作用域里有没有一个y,
: 以此类推。举个例子:
: // x和y被加到全局作用域,因为后面有var x和var y的声明

X****r
发帖数: 3557
8
You're welcome, but I'm sadden that I typed all these in vain, that you
don't seem understand more than you did before my post.
There is nothing unusual about a callback function. It just execute at
a different time than it is defined. But this should be expected -- you
don't really want to deleteRow immediately inside the loop, do you?
And your callback function does not have a parameter -- 'name' is
not a parameter as you implied. If it were, and you pass the
corresponding argument, then yes i

【在 N***m 的大作中提到】
: Thank you very much for such a detailed reply. I have little experience
: with call back function before.
: So I guess the question is actually due to the call back function
: "deleteRow(_name)", whose parameter "_name" in this case only points
: to the outer scope variable "name". And since the event function is called
: back later, "_name" gets updated only after the loop finishes. Initially,
: I naively think that once the call back function is attached to a
: onclick event, its parameter is assigned to t

N***m
发帖数: 4460
9

I think I understand this part:)
Now I get confused with parameters and arguments. It seems that you
refer them to different things? I have the bad habbit of mixing them.
(Because in physics, we always say some measurements depend on some physical
parameters although such measurements actually take the functional form
of some parameters, i.e. arguments.) Maybe I misunderstood your point.
Pardon me if I brought any confusion here:)
Now, for the last question, it is really beyond my capability. F

【在 X****r 的大作中提到】
: You're welcome, but I'm sadden that I typed all these in vain, that you
: don't seem understand more than you did before my post.
: There is nothing unusual about a callback function. It just execute at
: a different time than it is defined. But this should be expected -- you
: don't really want to deleteRow immediately inside the loop, do you?
: And your callback function does not have a parameter -- 'name' is
: not a parameter as you implied. If it were, and you pass the
: corresponding argument, then yes i

X****r
发帖数: 3557
10

physical
People usually refer parameters and arguments as the same thing.
This is fine. However, strickly speaking, parameters are part of the
function being called, while arguments are the values supplied by the
caller when invoking the function. e.g.
function(a) {
// a is a parameter of function f()
}
f(1); // 1 is the (only) argument used to call f()
In my previous post I needed to distinguish the difference, thus I
used these two different term.
to
reference?
// Let's call the out-most sc

【在 N***m 的大作中提到】
:
: I think I understand this part:)
: Now I get confused with parameters and arguments. It seems that you
: refer them to different things? I have the bad habbit of mixing them.
: (Because in physics, we always say some measurements depend on some physical
: parameters although such measurements actually take the functional form
: of some parameters, i.e. arguments.) Maybe I misunderstood your point.
: Pardon me if I brought any confusion here:)
: Now, for the last question, it is really beyond my capability. F

相关主题
问一个C++问题:default parameter and overriding/inheritanc (转载)问个问题
question about using Hive parameter (转载)[合集] C/C++ calling function by name
一个面试题目,用C实现[合集] matlab函数的返回变量
进入Programming版参与讨论
N***m
发帖数: 4460
11
醍醐灌顶,豁然开朗阿!
多谢大虾!
这样的trick我还没在书上看到过,赞一下!

【在 X****r 的大作中提到】
:
: physical
: People usually refer parameters and arguments as the same thing.
: This is fine. However, strickly speaking, parameters are part of the
: function being called, while arguments are the values supplied by the
: caller when invoking the function. e.g.
: function(a) {
: // a is a parameter of function f()
: }
: f(1); // 1 is the (only) argument used to call f()

m*****k
发帖数: 1864
12
瞻仰强人。我迷迷糊糊会用,但是讲不清。
f*****Q
发帖数: 1912
13
尽管俺系土人,而且很多年不写程序了。但是还想说一句,基础不打牢固,后面很难混
的。

【在 N***m 的大作中提到】
: 醍醐灌顶,豁然开朗阿!
: 多谢大虾!
: 这样的trick我还没在书上看到过,赞一下!

N********n
发帖数: 8363
14

If the language itself has some inherent confusing garbage inside then it
is really not worth building a foundation on top of it.

【在 f*****Q 的大作中提到】
: 尽管俺系土人,而且很多年不写程序了。但是还想说一句,基础不打牢固,后面很难混
: 的。

f*****Q
发帖数: 1912
15
存在就是有道理的。当年土人俺也奋过,您的心情俺理解。

【在 N********n 的大作中提到】
:
: If the language itself has some inherent confusing garbage inside then it
: is really not worth building a foundation on top of it.

s******e
发帖数: 493
16
or you can redefine you deleteRow method. Instead of passing the id, get it
on the fly. for example:
use sth like DOM.table.getSelectedRowId (not the real code).
N***m
发帖数: 4460
17
I will try it later.
It always costs me hours to make a code run:)

it

【在 s******e 的大作中提到】
: or you can redefine you deleteRow method. Instead of passing the id, get it
: on the fly. for example:
: use sth like DOM.table.getSelectedRowId (not the real code).

f*****Q
发帖数: 1912
18
while( vTargetTd.nodeName != "TH" ) vTargetTd = vTargetTd.parentNode;
我原来用过的类似功能的代码。

it

【在 s******e 的大作中提到】
: or you can redefine you deleteRow method. Instead of passing the id, get it
: on the fly. for example:
: use sth like DOM.table.getSelectedRowId (not the real code).

1 (共1页)
进入Programming版参与讨论
相关主题
C++ online Test 又一题 (转载)Question: How to store initialize arguments?
javascript function-ask for help问一个C++问题:default parameter and overriding/inheritanc (转载)
这个function pointer最后的那个int是什么意思?question about using Hive parameter (转载)
template metaprogramming 的问题一个面试题目,用C实现
Default function template arguments问个问题
static如何作为函数?[合集] C/C++ calling function by name
感觉学scala不学haskell是不行的[合集] matlab函数的返回变量
one more interview questionhelp: matlab integral function
相关话题的讨论汇总
话题: function话题: name话题: deleterow话题: var话题: 作用域