0
   

Javascript functions objects

 
 
kdowney
 
Reply Tue 5 Jul, 2011 02:37 am
Hi,

Can anyone explain why this is?

functions can be declared in the constructor or in the prototype or on the object after the object has been initialised.

functions can not be declared on the object previous to initialisation, it is not available after iniaialisation.

e.g.

var Person = function () {
this.ctx = 'this is some other stuff'; // Public ;
var stuff = 'this is some stuff'; // Private

this.getThisSuff = function () { // this works
return stuff;
};
};

Person.getMyOtherStuff = function () { // this doesnt work

return (function () {return this.ctx;})();

};

Person.prototype.getMyStuff = function () { // this works

return this.ctx;
};


Person.StuffHolder = 'more stuff'; // this works


var p = new Person();

alert('1 ' + p.getThisSuff()); // works

alert('2 ' + p.getMyStuff()); // works

alert('3 ' + p.getMyOtherStuff()); // dont work, why so?
  • Topic Stats
  • Top Replies
  • Link to this Topic
Type: Question • Score: 0 • Views: 586 • Replies: 2
No top replies

 
kdowney
 
  1  
Reply Tue 5 Jul, 2011 02:39 am
@kdowney,
This is the example I wanted to post rather:

var Person = function () {
this.ctx = 'this is some other stuff'; // Public ;
var stuff = 'this is some stuff'; // Private

this.getThisSuff = function () { // this works
return stuff;
};
};

Person.getMyOtherStuff = function () { // this doesnt work

return (function () {return this.ctx;})();

};

Person.prototype.getMyStuff = function () { // this works

return this.ctx;
};


Person.StuffHolder = 'more stuff'; // this works


var p = new Person();

alert('1 ' + p.getThisSuff()); // works

alert('2 ' + p.getMyStuff()); // works

alert('3 ' + p.getMyOtherStuff()); // dont work, why so?
kdowney
 
  1  
Reply Tue 5 Jul, 2011 03:32 am
@kdowney,
inialising the object or 'new'ing it up executes the constructor. 'getMyOtherStuff ' was not declared in the constructor and so is not considered part of the instance of the object.

Is essentially static?


0 Replies
 
 

Related Topics

 
  1. Forums
  2. » Javascript functions objects
Copyright © 2024 MadLab, LLC :: Terms of Service :: Privacy Policy :: Page generated in 0.03 seconds on 05/22/2024 at 11:57:42