在对象中用作 属性 :引用函数内部的其他属性
functions as a property in objects : referencing other properties inside of your function
如果我有一个函数作为对象的 属性,函数闭包规则是否仍然适用?我记得读过函数是对象,但我也明白对象不是函数。
更具体地说,我可以在不引用该函数中的对象的情况下获取和编辑同一对象内的其他属性吗?这是一个例子:
someObj = {
property : 44,
calculate : function(){
property * moreproperties;
};
或者我这样做吗?
someObj = {
property : 44,
calculate : function(){
someObj.property * someObj.moreproperties;
};
您可以使用 this
关键字
someObj = {
property : 44,
calculate : function(){
this.property * this.moreproperties;
};
如果您有另一个函数,例如 jQuery 中事件的回调,请将 this
存储到变量
someObj = {
property : 44,
calculate : function(){
var parent = this;
$('#some-element').click(function() {
parent.something * parent.somethingElse
});
};
如果我有一个函数作为对象的 属性,函数闭包规则是否仍然适用?我记得读过函数是对象,但我也明白对象不是函数。
更具体地说,我可以在不引用该函数中的对象的情况下获取和编辑同一对象内的其他属性吗?这是一个例子:
someObj = {
property : 44,
calculate : function(){
property * moreproperties;
};
或者我这样做吗?
someObj = {
property : 44,
calculate : function(){
someObj.property * someObj.moreproperties;
};
您可以使用 this
关键字
someObj = {
property : 44,
calculate : function(){
this.property * this.moreproperties;
};
如果您有另一个函数,例如 jQuery 中事件的回调,请将 this
存储到变量
someObj = {
property : 44,
calculate : function(){
var parent = this;
$('#some-element').click(function() {
parent.something * parent.somethingElse
});
};