【发布时间】:2015-07-30 02:32:59
【问题描述】:
我试图理解为什么声明重复函数在语句执行后会影响它。
就好像 JavaScript 首先读取所有函数,不管放置/控制流,然后执行console.log 表达式。示例:
function Question(x, y) {
this.getAnswer = function() {
return 42;
};
};
var tony = new Question('Stack','Overflow');
console.log(tony.getAnswer()); // 42, as expected.
// If the following 2 lines are uncommented, I get an error:
// function Question(x, y) {
// };
错误是:
未捕获的类型错误:tony.getAnswer 不是函数
但是当 JavaScript 运行 console.log 语句时,它如何知道它还不是一个函数,因为 Person 类直到 console.log 之后的行 才会被覆盖?
【问题讨论】:
标签: javascript object