[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 376
  • Last Modified:

define an object by a function?

I am a javascript novice trying to learn ajax.

in a sample code, I see the statement
Ext.grid.CheckColumn = function(config){
    Ext.apply(this, config);
    if(!this.id){
        this.id = Ext.id();
    }
    this.renderer = this.renderer.createDelegate(this);
};

CheckColum is clearly meant to be an object, because else where I see the statement
 var checkColumn = new Ext.grid.CheckColumn({
       header: "Indoor?",
       dataIndex: 'indoor',
       width: 55
    });

I looked up a javascript manual, but can not find where it explains how a function constitute an object (a var). Also, I don't know what to make of the arg config.

as you see in the above constructor call, no config param is ever supplied.
0
bhomass
Asked:
bhomass
  • 4
  • 3
2 Solutions
 
frindoCommented:
That is an implementation of the Javascript class functionality. Javascript, although used by most for little modifications to websites actually has implementable object classes whereby you can define functions of an object and then create the object later in the script and access the functions that you created for it. You can also add functions to existing objects such as String.
0
 
bhomassAuthor Commented:
I am more confused now. are you saying CheckColumn is a function of Ext.grid, and not an object? clearly that's not true.

so, if CheckColumn is an object class, then which is the function you are talking about? what is the name of the function?
0
 
frindoCommented:
Sorry to confuse, let me try to explain it again.

Javascript has a remarkably versatile object system. Functions can be objects if you want them to be, but let me explain with a simpler example:

Let's say you want to create a counter, you could do the following:

function counter() {this.start();};

Basically that defines an object, counter, which on creation runs its native start function. But we have to define the start function.

count.prototype.start = function()
{    
    alert('I have started!');
};

Would create a function for the object counter which it would run on creation. Your script actually has a similar functionality, except it is an object of another object so it gets a bit more confusing.

So the official answer is that objects in javascript are defined as functiony type things that can have subfunctions.

Ext.grid.CheckColumn.prototype ={
init : function(grid){
this.grid = grid;
this.grid.on('render', function(){
var view = this.grid.getView();
view.mainBody.on('mousedown', this.onMouseDown, this);
}

The code above (I found that by googling the location of your sample script) does the same thing that I did with my prototype script except arranged it in a different order.

Hope that makes things a bit clearer.
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
bhomassAuthor Commented:
ok, so a javascript object can be expressed as a function. and I assume prototype is meant to the template which is cloned when the object is instantiated.

since you have located the entire code that puzzles me (thanks much to google), please help me with all the areas I marked with ?

1. where is the config arg in Ext.grid.CheckColumn = function(config) from? I only see one place checkSum is called, but without any input arg.
2. does 'this" inside the function mean CheckSum itself? or the object which contains this CheckSum call? e.g. in java, it would mean the class containing the method.
0
 
frindoCommented:
1)  
In,

var checkColumn = new Ext.grid.CheckColumn({
       header: "Indoor?",
       dataIndex: 'indoor',
       width: 55
    });

The config arg is "{
       header: "Indoor?",
       dataIndex: 'indoor',
       width: 55
    }"

It takes an array of data.

2) "this" refers to the actual object which has been created.

For example, here it applies the created object to the Ext parent object:

Ext.apply(this, config);
0
 
bhomassAuthor Commented:
wonderful. I feel like I made a breakthru in understanding javascript code.
0
 
frindoCommented:
You're not the only one! I looked up what Ext really was and I had no idea that such an incredible javascript library existed...

Thanks!
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 4
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now