我是javascript的新手。 我想用这些方法改变方形的长度。但它不起作用。我找不到,为什么。 Plz帮助,而不仅仅是投票。
// to change sideLength
this.set_sideLength = function(new_sideLength) {
this.sideLength = new_sideLength;
};
var square = new Object();
square.sideLength = 6;
square.set_sideLength = set_sideLength;
// Calculate perimeter
square.calcPerimeter = function() {
return this.sideLength * 4;
};
var p = square.calcPerimeter();
// output
console.log("Perimeter is: " + p + ", if side length is "+ sideLength);
// New ouptput with new sideLength
square.set_sideLength(10);
console.log("Perimeter is: " + p + ", if side length is "+ sideLength);
答案 0 :(得分:1)
功能似乎很好。您执行的console.logs是旧值,而不是访问对象上的属性。将sideLength
设置为10后,您不会为calcPerimeter
请求新的sideLength
}。您需要访问sideLength
对象上的square
,例如square.sideLength
// to change sideLength
this.set_sideLength = function(new_sideLength) {
this.sideLength = new_sideLength;
};
var square = new Object();
square.sideLength = 6;
square.set_sideLength = set_sideLength;
// Calculate perimeter
square.calcPerimeter = function() {
return this.sideLength * 4;
};
var p = square.calcPerimeter();
// output
console.log("Perimeter is: " + p + ", if side length is "+ square.sideLength);
// New ouptput with new sideLength
square.set_sideLength(10);
p = square.calcPerimeter();
console.log("Perimeter is: " + p + ", if side length is "+ square.sideLength);
就像@Felix Kling提到的那样,你应该改变
// from
this.set_sideLength = function(new_sideLength) {
this.sideLength = new_sideLength;
};
// to
function set_sideLength(new_sideLength) {
this.sideLength = new_sideLength;
};
根据this
实际上的内容,将函数分配给this
上的属性可能会产生不同的影响。在您的上下文中可能代表window
对象。这就是为什么以下工作原理。
```
square.set_sideLength = set_sideLength;
```
通常,向窗口对象添加属性被认为是不好的做法。因此,您最好使用其他方法,并将整个事物包装成IIFE。
喜欢这样
(function () {
'use strict';
// to change sideLength
function set_sideLength(new_sideLength) {
this.sideLength = new_sideLength;
}
var square = new Object();
square.sideLength = 6;
square.set_sideLength = set_sideLength;
// Calculate perimeter
square.calcPerimeter = function calcPerimeter() {
return this.sideLength * 4;
};
var p = square.calcPerimeter();
// output
console.log("Perimeter is: " + p + ", if side length is "+ square.sideLength);
// New ouptput with new sideLength
square.set_sideLength(10);
p = square.calcPerimeter();
console.log("Perimeter is: " + p + ", if side length is "+ square.sideLength);
})();
答案 1 :(得分:0)
您还没有第二次调用calcPerimeter
函数,sideLength
应该调用square.sideLength
。
// to change sideLength
this.set_sideLength = function(new_sideLength) {
this.sideLength = new_sideLength;
};
var square = new Object();
square.sideLength = 6;
square.set_sideLength = set_sideLength;
// Calculate perimeter
square.calcPerimeter = function() {
return this.sideLength * 4;
};
var p = square.calcPerimeter();
// output
document.write("Perimeter is: " + p + ", if side length is "+ square.sideLength);
// New ouptput with new sideLength
square.set_sideLength(10);
var p = square.calcPerimeter();
document.write("<br/>Perimeter is: " + p + ", if side length is "+ square.sideLength);
&#13;
答案 2 :(得分:0)
function Square(){}
Square.prototype.setSideLength = function( sideLength ) {
this.sideLength = sideLength;
};
var square = new Square();
square.sideLength = 6;
Square.prototype.calcPerimeter = function() {
return this.sideLength * 4;
};
console.log("Perimeter is: " + square.calcPerimeter() +
", if side length is "+ square.sideLength);
square.setSideLength(10);
console.log("Perimeter is: " + square.calcPerimeter() +
", if side length is "+ square.sideLength);
Console: