ExtJS 3:创建自定义类的两种方式:有什么区别? [英] ExtJS 3: Two ways of creating custom class: what's the difference?

查看:160
本文介绍了ExtJS 3:创建自定义类的两种方式:有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试学习ExtJS和面向对象的JavaScript。我已经看到人们以几种方式在自定义命名空间中定义类。这两种方法有什么区别?

I'm trying to learn ExtJS and object-oriented JavaScript in general. I've seen people defining classes in custom namespaces in a couple of ways. What's the difference between these two methods?

方法1

Ext.ns('myapp.cars');
(function(){
    var Car = Ext.extend(Object, {
       //...
    })

    myapp.cars.Car = Car;
})()

方法2

Ext.ns('myapp.cars');
myapp.cars.Car = Ext.extend(Object, {
       //...
});

方法2更容易阅读,需要较少的代码;有没有什么方法1更好?谢谢!

Method 2 is easier to read and requires less code; is there any reason Method 1 is better? Thanks!

推荐答案

基本上是一样的,除了你可以在第一种方法的self-exectuing函数中使用私有变量,而您只能在第二个定义全局变量。

It's basically the same, except that you could use private variables in the self-exectuing function of the first method, while you can only define global variables in the second one.

例如:

Ext.ns('myapp.cars');
(function(){

    var carInfo = {
      goodEngine: true
    };

    var Car = Ext.extend(Object, {
       info: carInfo
    });

    myapp.cars.Car = Car;
})()

// carInfo is undefined here, so this will give an error
alert( carInfo.goodEngine );

所以,第一种方法是非常有用的,如果你使用一个变量,你不会

So, the first method is quite useful if you work with a bunge of variables that you won't use later on.

这篇关于ExtJS 3:创建自定义类的两种方式:有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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