更好地理解面向对象的JavaScript架构 [英] Understanding better Javascript OOP architecture

查看:129
本文介绍了更好地理解面向对象的JavaScript架构的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

由于我通过AngularjsUI插件中,我绊了一些code,它给我看了一些例子看我的Javascript knowdledge相当改善的:

As i read through some examples of Angularjs' UI add-on, i've stumbled over some code that showed me that my knowdledge of Javascript is quite improvable:

下面是一个角提供商的内部的类:

The following is a class inside of an Angular provider:

function Dialog(opts) {

            var self = this, options = this.options = angular.extend({}, defaults, globalOptions, opts);
            this._open = false;

            this.backdropEl = createElement(options.backdropClass);
            if(options.backdropFade){
                // ...
            }

            this.handleLocationChange = function() {
                self.close();
            };

            // more functions
        }

pretty简单。但是,在课堂之外,也有原型的功能,例如上面调用的close()

Dialog.prototype.open = function(templateUrl, controller){
            var self = this, options = this.options;

            // .. some code
        };

现在我不明白为什么这个函数声明为原型,但 handleLocationChange 里面的类本身。

Now i do not understand why that function is declared as a prototype, but handleLocationChange inside the class itself.

我如何决定选择哪种方法?

How do i decide which method to choose?

完整的要点可以在这里找到

The full gist can be found here

推荐答案

考虑以下两种情况:

Dialog.prototype.open = function...

Dialog.open = function....

第一种情况 - 通过调用新的对话框()都会有这样的打开函数

二案无关与对话的对象,认为这是静电功能。

Second case has nothing to do with dialog objects, consider it as static function.

修改

发现了一个伟大的答案在这里:<一href=\"http://stackoverflow.com/questions/1635116/javascript-class-method-vs-class-prototype-method\">javascript-class-method-vs-class-prototype-method

found a great answer here : javascript-class-method-vs-class-prototype-method

这篇关于更好地理解面向对象的JavaScript架构的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆