在knockoutjs中声明视图模型
我是 JavaScript 和 knockoutjs 的新手。这可能是最基本的问题,我希望在这里问的问题不会太愚蠢......
在很多文档中,我看到视图模型以两种不同的方式声明。有时它们被声明为函数,有时它们是普通对象。有什么区别以及我将使用它们的场景是什么。
var viewModel = {
property: ko.observable()
}
或者
var viewModel = function(){
this.property = ko.observable()
}
抱歉,如果这是一个愚蠢的问题......
I am new to JavaScript and knockoutjs. This is probably the most basic of questions and I hope not too stupid a one to ask here...
On a lot of the documentation I see the view models being declared in two different ways. Sometimes they are declared as functions and at other times they are plain objects. What is the difference and what are the scenarios in which I would use each.
var viewModel = {
property: ko.observable()
}
Or
var viewModel = function(){
this.property = ko.observable()
}
Apologies if this is a dumb question....
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
第二个示例是 JavaScript 中的构造函数 - 由于 JavaScript 没有“类”,因此这就是实现“类”实例创建的方式。
更多信息:http://www.javascriptkit.com/javatutors/oopjs2.shtml
The second example is a constructor function in JavaScript - since JavaScript doesn't have "classes", that's how you implement "classlike" instance-creation.
More info: http://www.javascriptkit.com/javatutors/oopjs2.shtml