admin管理员组文章数量:1410717
Can someone please explain why I get the results I do in each of the following situations? I wish to understand why the outes are what they are regarding how JavaScript works with scope, if this is what the issue is. In the first example, my code functions properly.
var Employees = function(name, salary) {
this.name = name;
this.salary = salary;
this.addSalary = addSalaryFunction;
this.getSalary = function() {
return this.salary;
};
};
var addSalaryFunction = function(addition) {
this.salary = this.salary + addition;
};
var ceo = new Employees("Chris", 400000);
ceo.addSalary(20000);
document.write(ceo.getSalary());
If I move the addSalaryFunction
into the Employees
function, and below this.addSalary
I get the Uncaught TypeError.
var Employees = function(name, salary) {
this.name = name;
this.salary = salary;
this.addSalary = addSalaryFunction;
this.getSalary = function() {
return this.salary;
};
var addSalaryFunction = function(addition) {
this.salary = this.salary + addition;
};
};
var ceo = new Employees("Chris", 400000);
ceo.addSalary(20000);
document.write(ceo.getSalary());
But if I move the addSalaryFunction
above this.addSalary
if works properly again. Although my IDE tells me that my local variable addSalaryFunction
is redundant.
var Employees = function(name, salary) {
this.name = name;
this.salary = salary;
var addSalaryFunction = function(addition) {
this.salary = this.salary + addition;
};
this.addSalary = addSalaryFunction;
this.getSalary = function() {
return this.salary;
};
};
var ceo = new Employees("Chris", 400000);
ceo.addSalary(20000);
document.write(ceo.getSalary());
Can someone please explain why I get the results I do in each of the following situations? I wish to understand why the outes are what they are regarding how JavaScript works with scope, if this is what the issue is. In the first example, my code functions properly.
var Employees = function(name, salary) {
this.name = name;
this.salary = salary;
this.addSalary = addSalaryFunction;
this.getSalary = function() {
return this.salary;
};
};
var addSalaryFunction = function(addition) {
this.salary = this.salary + addition;
};
var ceo = new Employees("Chris", 400000);
ceo.addSalary(20000);
document.write(ceo.getSalary());
If I move the addSalaryFunction
into the Employees
function, and below this.addSalary
I get the Uncaught TypeError.
var Employees = function(name, salary) {
this.name = name;
this.salary = salary;
this.addSalary = addSalaryFunction;
this.getSalary = function() {
return this.salary;
};
var addSalaryFunction = function(addition) {
this.salary = this.salary + addition;
};
};
var ceo = new Employees("Chris", 400000);
ceo.addSalary(20000);
document.write(ceo.getSalary());
But if I move the addSalaryFunction
above this.addSalary
if works properly again. Although my IDE tells me that my local variable addSalaryFunction
is redundant.
var Employees = function(name, salary) {
this.name = name;
this.salary = salary;
var addSalaryFunction = function(addition) {
this.salary = this.salary + addition;
};
this.addSalary = addSalaryFunction;
this.getSalary = function() {
return this.salary;
};
};
var ceo = new Employees("Chris", 400000);
ceo.addSalary(20000);
document.write(ceo.getSalary());
Share
Improve this question
asked May 17, 2013 at 13:11
chris_schris_s
1,0514 gold badges14 silver badges28 bronze badges
4 Answers
Reset to default 7It's because you're trying to assign the function before it was created.
this.addSalary = addSalaryFunction; // there's no function yet
//...
var addSalaryFunction = function(addition) { // now there is, but too late
this.salary = this.salary + addition;
};
When you moved the variable assignment above the this.addSalary = addSalaryFunction
, you're now creating the function before trying to reference it.
var addSalaryFunction = function(addition) { // here's the function
this.salary = this.salary + addition;
};
this.addSalary = addSalaryFunction; // now we can assign it
If you had used function declaration syntax instead, the first version would work, because the function declarations are "hoisted" (as they say) to the top of the variable scope.
this.addSalary = addSalaryFunction; // This now works because of the magic below
//...
// This is magically hoisted to the top
function addSalaryFunction(addition) {
this.salary = this.salary + addition;
}
Second method does not work because addSalaryFunction is being referenced before it has been declared.
You could eliminate some code and just declare:
this.addSalary = function(addition) {
this.salary = this.salary + addition;
}
In a much simpler form:
var foo = function() {
var x = y;
var y = 2;
return x;
};
var bar = function() {
var y = 2;
var x = y;
return x;
};
Obviously, bar()
will return 2. foo
, however, gets undefined
when it looks for a value of y
on the first line, so returns undefined
. While variable declarations are hoisted to the top of their scope, variable initialisations are not.
It's a specific hoisting problem. Have a look at this explanation: http://www.sitepoint./back-to-basics-javascript-hoisting/
本文标签:
版权声明:本文标题:javascript - Explain this Uncaught TypeError: Property 'x' of object [object Object] is not a function - Stack O 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745012417a2637631.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论