【问题标题】:What is the difference between these two coding approach in knockout js [duplicate]淘汰赛js中这两种编码方法有什么区别[重复]
【发布时间】:2013-12-04 08:12:26
【问题描述】:

看看这两种方法并告诉我有什么区别。什么时候应该使用? 在第一种情况下,将创建实例,但在第二种情况下,ko.applyBindings() 时不会创建任何实例,所以这里是代码。

function MyViewModel() {
         var self = this;
         self.lastInterest = ko.observable();
         self.places = ko.observableArray(['London', 'Paris', 'Tokyo']);

         // The current item will be passed as the first parameter, so we know which place was hovered over
         self.logMouseOver = function(place) {
             self.lastInterest(place);
         }
     }
     ko.applyBindings(new MyViewModel());


var viewModel = {
        detailsEnabled: ko.observable(false),
        enableDetails: function() {
            this.detailsEnabled(true);
        },
        disableDetails: function() {
            this.detailsEnabled(false);
        }
    };
    ko.applyBindings(viewModel);

【问题讨论】:

  • 另一个小问题是knockout js是google开发的库,因为当我去knockout js论坛时,url看起来像groups.google.com/group/knockoutjs。

标签: javascript knockout.js


【解决方案1】:

第一个代码部分是一个函数,但第二个是基于类的模块设计模式代码,您可以在第二个代码中访问参数!

看看这个:

http://www.adequatelygood.com/JavaScript-Module-Pattern-In-Depth.html

【讨论】:

    猜你喜欢
    • 2015-08-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-08-12
    • 1970-01-01
    • 2013-09-08
    • 2019-03-31
    相关资源
    最近更新 更多