ExtJS 3: Two ways of creating custom class: what's the difference?

前端 未结 2 2190
执念已碎
执念已碎 2021-02-20 00:37

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

2条回答
  •  [愿得一人]
    2021-02-20 00:53

    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.

    For example:

    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.

提交回复
热议问题