能否請您解釋一下什麼是MVC /觀測技術類似物在兩種情況下:MVC /觀察員和不可變的數據結構
- 不可變對象(OOP風格)
- 不可變的數據(功能型)
例如,讓我們考慮一下下面這個簡單的GUI示例(你可以嘗試住在這裏http://tinkerbin.com/0XDHRXIl點擊「運行」按鈕啓動,等待2秒,文本顯示)
它是用JavaScript的編譯,因爲它很容易發揮和MVC /觀察員是很自然的把它
// Model containing the data.
var Post = Backbone.Model.extend({})
var PostView = Backbone.View.extend({
initialize: function() {
// Registering view rendering method as
// an observer on the model.
this.model.on('all', this.render.bind(this))
},
// Every time state of model changes
// this method will be called.
render: function() {
// Printing value of model.text attriubute.
this.$el.html(this.model.get('text'))
return this
}
})
// Now, any time the model is updated the view will be also
// automatically updated.
post.set({text: "hello, it's me"})
但我不太明白如何做永恆OOP和功能性的風格相同,有哪些途徑?