Method and Function in Constructor
This question already has an answer here:
All methods are functions; not all functions are methods. Using your example:
function myObj() {
this.result = "RESULT: ";
// is this a method or a function ?
function innerFunc(a, b) {
return a + b;
//alert(this.result) not possible
}
this.sum = function(a, b) {
alert(this.result + innerFunc(a, b));
}
}
If you did var x = new myObj();
you would have x
that has two properties: result
, which is set to "RESULT: ", and sum
, which is a function that takes 2 args.
However, it would not have a property on it called innerFunc
. That is solely available from inside the x
, ie either in the constructor or a method, eg sum
.
So x.sum
is a method, which is a function, but innerFunc
is a function which is not a method.
上一篇: JAVA中的方法和函数有什么不同?
下一篇: 构造函数中的方法和函数