【发布时间】:2014-05-30 01:16:05
【问题描述】:
下面的代码是创建一个字典(foo),然后在字典中添加一个方法(bar)和属性名称ack。
var foo = {};
foo.bar = function(){
this.ack=3;
};
foo.bar();
在 python 中,如果我尝试做同样的事情,
>>> foo = {}
>>> def f():
this.ack=3
>>> foo.bar = f
Traceback (most recent call last):
File "<pyshell#4>", line 1, in <module>
foo.bar = f
AttributeError: 'dict' object has no attribute 'bar'
我的问题:
JavaScript 如何允许方法(
bar)成为字典的成员,而不是(键,值)对?另外,JavaScript如何让值为3的名称
ack(这不是键值)成为字典的成员?
【问题讨论】:
-
有苹果,也有橘子 :)
-
@thefourtheye 我没找到你
标签: javascript python dictionary