1

Can anyone tell me if the following example classes are equivalent, with respect to the class properties. One is defined within the constuctor and one outside.

Example 1

function Test(input)
{
    this.output = input ;
    Test.WELCOME = "Hello ;
}

Example 2

function Test(input)
{
    this.output = input ;        
}

Test.WELCOME = "Hello ;

I have been using the second type but both seems to work.

Cheers

ZosoOfZep
  • 51
  • 1
  • 4
  • Related: [Class keyword in Javascript](http://stackoverflow.com/questions/1728984/class-keyword-in-javascript). Zoso - Where is this JavaScript running? (probably not a browser) – Kobi Nov 30 '11 at 11:28
  • unless you are in some very early ES.Next alpha engine, there is no used `class` keyword in javascript. – jAndy Nov 30 '11 at 11:28
  • sorry mistyped class should have been function. These are simulated classes not true class. – ZosoOfZep Nov 30 '11 at 11:32
  • 1
    @Kobi Ahh, interesting! Cool. – Pekka Nov 30 '11 at 11:35

2 Answers2

4

I'm pretty sure neither of those does what you really want it to do. The usual way this inside of Test is something useful will be when instantiating it using new. Setting a property on Test itself doesn't have any effect on objects created from it using x = new Test('someinput'). What you would want to do is Test.prototype.WELCOME = 'Hello' and instances of Test would have that property in their [[proto]] tree and then be able to make use of it.

function Test(input){
  this.output = input;
}
Test.prototype.WELCOME = 'Hello';

var instanceOfTest = new Test('whatever');

// produces this structure:
instanceOfTest => {
  output: 'whatever',
  __proto__: {
    WELCOME: 'Hello'
  }
}

console.log(instanceOfTest.WELCOME); // 'Hello'
  • If you use prototype each object will have an instance of WELCOME. If you use it as per my example the property is associated with the 'class' not the instance. – ZosoOfZep Nov 30 '11 at 12:38
  • Incorrect. Every instance created using `new` will reference one singular `__proto__`. That property will only exist in one place. `Test` is the constructor, it isn't in the `[[proto]]` tree. Look at the functions on `Object` compared to `Object.prototype`. Then look what hidden methods `x` has when doing `var x = {};`. –  Nov 30 '11 at 12:41
  • I understand that each object has a prototype object. All the properties of the prototype will appear as properties of the object. If you look at an instance of my example WELCOME will not be one of the properties. You can only access it via the class name. – ZosoOfZep Nov 30 '11 at 13:00
3

Assuming you mean:

function Test(input)
{
  this.output = input;
  Test.WELCOME = "Hello";
}

Example 2

function Test(input)
{
  this.output = input ;        
}
Test.WELCOME = "Hello";

Then there will be no difference in terms of the properties of any new Test objects, however in Example 1 you will be assigning a WELCOME property to the Test function on each call to the method, whereas in Example 2 you will only assign this property once.

Rich O'Kelly
  • 41,274
  • 9
  • 83
  • 114