来自 C++/Java 背景,CoffeeScript 中的 OOP-Inheritance 让我感到困惑。
考虑以下示例:
class BaseClass
arr: []
addItem: (item) =>
@arr.push item
class SubClassA extends BaseClass
constructor: ->
@addItem("fromSubA")
class SubClassB extends BaseClass
console.log "Case 1"
instB = new BaseClass()
instA = new SubClassA()
console.log "instA: #{JSON.stringify instA.arr}"
console.log "instB #{JSON.stringify instB.arr}"
console.log "Case 2"
instB = new SubClassB()
instA = new SubClassA()
console.log "instA: #{JSON.stringify instA.arr}"
console.log "instB #{JSON.stringify instB.arr}"
console.log "Case 3"
instB = new SubClassB()
instA = new SubClassA()
console.log "instA: #{JSON.stringify instA.arr}"
console.log "instB #{JSON.stringify instB.arr}"
tinkerbin.com 中的输出:
Case 1
instA: ["fromSubA"]
instB ["fromSubA"]
Case 2
instA: ["fromSubA","fromSubA"]
instB ["fromSubA","fromSubA"]
Case 3
instA: ["fromSubA","fromSubA","fromSubA","Added manually, only to instB"]
instB ["fromSubA","fromSubA","fromSubA","Added manually, only to instB"]
Iow:基类的“arr”实例属性的行为或多或少类似于静态属性;如果我在一个实例中更改数组,它在另一个实例中也会更改。为什么这个数组在实例之间共享?
令人困惑的是,字符串属性没有表现出这种行为:
class BaseClass
property: "From Base"
class SubClassA extends BaseClass
constructor: ->
@property = "fromSubClassA"
class SubClassB extends BaseClass
document.write "Case 1<br>"
instB = new BaseClass()
instA = new SubClassA()
document.write "#{instA.property} <br>"
document.write "#{instB.property} <br><br>"
document.write "Case 2<br>"
instB = new SubClassB()
instA = new SubClassA()
instA.property = "test"
document.write "#{instA.property} <br>"
document.write "#{instB.property} <br>"
这段代码工作得很好,保持实例之间的“属性”属性隔离。
我显然在这里误解了一些东西。是什么导致阵列在实例之间共享?