2015-06-20 137 views
0

我是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

如果你看一下控制檯你就會知道爲什麼它不工作:'回覆ferenceError:sideLength未定義。 –

+0

是的。 sideLength = 6然後我試圖改爲10. –

+0

'square.sideLength = 6;'設置**對象**'square'的**屬性**'slideLength'。它沒有**創建**變量**'sideLength'。你真的認爲瀏覽器更可能出現錯誤,而不是你?如果控制檯顯示某個未定義的錯誤,那麼它沒有被定義。 –

回答

1

的功能顯得相當精緻。您執行的console.log是舊值,並且不訪問該對象上的屬性。將sideLength設置爲10後,您不會爲該sideLength請求新的calcPerimeter。而你需要訪問sideLengthsquare對象像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克林提到你或許應該改變

// 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; 

```

一般來說它被認爲是不好的做法,將屬性添加到window對象。因此,你會更好地使用其他方法,並且整個事情都是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); 
 
})();

+0

如果代碼引發錯誤,代碼如何處理? 'this.set_sideLength = function(new_sideLength){}'真的應該是'function set_sideLength(new_sideLength){}'btw –

+0

這些函數是selfs。也許我應該澄清,更好:) –

+0

@FelixKling,因爲他稱爲功能的方式是正確的,只有一件事需要修復。削減OP一些鬆懈。 –

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);

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);

控制檯:

Firefox console