方法的javascript属性
在javascript中是否可以在方法内部设置属性 比如说方法的javascript属性,javascript,oop,Javascript,Oop,在javascript中是否可以在方法内部设置属性 比如说 function Main() { this.method = function() { this.parameter = 'something_relevant' } } var p = new Main() p.method() console.log(p.method.parameter) 我尝试了这个,它记录为“未定义”。它是关于范围的吗?函数基本上是对象,所以只需将其设置为您得到的: this.m
function Main() {
this.method = function() {
this.parameter = 'something_relevant'
}
}
var p = new Main()
p.method()
console.log(p.method.parameter)
我尝试了这个,它记录为“未定义”。它是关于范围的吗?函数基本上是对象,所以只需将其设置为您得到的:
this.method = function() {
};
this.method.parameter = 'something_relevant';
另外,不要在表达式后删除分号。函数基本上是对象,所以只需将其设置为您所得到的:
this.method = function() {
};
this.method.parameter = 'something_relevant';
另外,不要在表达式后删除分号。在method()
这显示了方法内部的差异:
this.method = function() {
this.parameter = 'abc'; // Set parameter on the object on which method() is called
this.method.parameter = 'xyz'; // Set parameter on the object representing the method itself
};
这显示了在调用方法后访问属性的差异
p.method();
console.log(p.parameter); // Display property of the object p, equals 'abc'
console.log(p.method.parameter); // Display property of the function object representing method(), equals 'xyz'
您应该决定是需要函数对象上的属性还是p
对象上的属性。请注意,函数对象可能由Main()
构造函数创建的多个对象共享。因此,它的行为方式与C++、java等语言中的静态成员有点类似。
如果要使用在对象上定义的属性,则代码应类似于以下内容:
function Main() {
this.method = function() {
this.parameter = 'something_relevant'; // Set property on object on which method() is called.
};
}
var p = new Main();
p.method();
console.log(p.parameter); // Read property from object p.
function Main() {
this.method = function() {
this.method.parameter = 'something_relevant'; // Set property on function object representing method().
};
}
var p = new Main();
p.method();
console.log(p.method.parameter); // Read property from the function object.
如果要使用在表示method()
的函数对象上定义的属性,则代码应如下所示:
function Main() {
this.method = function() {
this.parameter = 'something_relevant'; // Set property on object on which method() is called.
};
}
var p = new Main();
p.method();
console.log(p.parameter); // Read property from object p.
function Main() {
this.method = function() {
this.method.parameter = 'something_relevant'; // Set property on function object representing method().
};
}
var p = new Main();
p.method();
console.log(p.method.parameter); // Read property from the function object.
在method()
这显示了方法内部的差异:
this.method = function() {
this.parameter = 'abc'; // Set parameter on the object on which method() is called
this.method.parameter = 'xyz'; // Set parameter on the object representing the method itself
};
这显示了在调用方法后访问属性的差异
p.method();
console.log(p.parameter); // Display property of the object p, equals 'abc'
console.log(p.method.parameter); // Display property of the function object representing method(), equals 'xyz'
您应该决定是需要函数对象上的属性还是p
对象上的属性。请注意,函数对象可能由Main()
构造函数创建的多个对象共享。因此,它的行为方式与C++、java等语言中的静态成员有点类似。
如果要使用在对象上定义的属性,则代码应类似于以下内容:
function Main() {
this.method = function() {
this.parameter = 'something_relevant'; // Set property on object on which method() is called.
};
}
var p = new Main();
p.method();
console.log(p.parameter); // Read property from object p.
function Main() {
this.method = function() {
this.method.parameter = 'something_relevant'; // Set property on function object representing method().
};
}
var p = new Main();
p.method();
console.log(p.method.parameter); // Read property from the function object.
如果要使用在表示method()
的函数对象上定义的属性,则代码应如下所示:
function Main() {
this.method = function() {
this.parameter = 'something_relevant'; // Set property on object on which method() is called.
};
}
var p = new Main();
p.method();
console.log(p.parameter); // Read property from object p.
function Main() {
this.method = function() {
this.method.parameter = 'something_relevant'; // Set property on function object representing method().
};
}
var p = new Main();
p.method();
console.log(p.method.parameter); // Read property from the function object.
这是因为如果在赋值后将控制台.log
放在此.method
中,则方法调用是异步的。@Krister Andersson:这里没有异步的,真的。这是因为方法调用是异步的,如果你在赋值后将控制台.log
放在这个.method
中,它就会工作。@Krister Andersson:这里没有异步,真的。谢谢你扩展你的答案,基本上我需要一个描述方法的属性,所以我可以说它与“类”相关(请原谅javascript上下文中的这个词)感谢您扩展您的答案,基本上我需要一个描述方法的属性,因此我可以说它与“类”相关(请原谅javascript上下文中的这个词)