Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

Method 1

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

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

Method 2

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

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

like image 470
Clint Harris Avatar asked Nov 07 '10 20:11

Clint Harris


People also ask

How to create class in Ext JS?

As per the syntax of Ext. define, the first parameter 'Student' is class name. The second parameter is a JavaScript object which contains name and getName(), and the third parameter (optional) is callback function which will be called after 'Student' class is created. So this way you can create custom class in Ext JS.

What is Sencha Ext JS?

February 17, 2022 | Eli M. Sencha Ext JS is the most comprehensive JavaScript framework for building data-intensive, cross-platform web and mobile applications for any modern device.

What creates ext?

Ext. create - to create an instance of a pre-defined class. - the class was defined using Ext. define - used to define the data and behavior of a component. which will be used later.

What function is used to extend from an existing object in Ext JS?

We can inherit a new class from existing class using extend keyword while defining a new class in Ext JS.


2 Answers

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.

like image 55
Harmen Avatar answered Oct 17 '22 09:10

Harmen


The following are practically equivalent:

var Car = Ext.extend(Object, {
   //...
});

myapp.cars.Car = Car;

... and:

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

In the first example you'd be using a temporary variable to hold a reference to the newly created object, which is then copied to myapp.cars.Car (the reference is copied, not the object). In the second example you'd be assigning the reference to the object directly to myapp.cars.Car.

The reason your first example was enclosed in the self-invoking anonymous function (function(){ })() is to limit the scope of that temporary variable. That is generally done in order not to pollute the global namespace with that Car variable. If there was an other Car variable defined elsewhere, it would not conflict with this one. Example:

var Car = "My Nice Car";

(function(){
    var Car = Ext.extend(Object, {
       //...
    });

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

alert(Car); // Still "My Nice Car"
            // No conflict with the `Car` variable in the self invoking function.
like image 21
Daniel Vassallo Avatar answered Oct 17 '22 10:10

Daniel Vassallo