Connecting Tech Pros Worldwide Forums | Help | Site Map

Memory concern with a Class in JavaScript.

dmjpro's Avatar
Lives Here
 
Join Date: Jan 2007
Location: India (West-Bengal)
Posts: 2,451
#1: Apr 24 '09
Have a look at my code ...

Expand|Select|Wrap|Line Numbers
  1. function MyClass(){
  2.  this.somePropperties = .....;
  3.  this.someMethods = function(){......}
  4. }
  5.  
  6. MyClass.prototype.someMethods = function(){......}
  7.  
In Java or C++ or in others Object Oriented language, methods or functions have only copy in memory somewhere else.
Yesterday i had a look at video tutorial on JavaScript OOPs, there they told some memory concerns about "this.someMethdos" and "Function.prototype.someMethods". Could anyone explain these two statements on the basis of Memory Concerns? ;)

Dormilich's Avatar
Moderator
 
Join Date: Aug 2008
Location: Leipzig, Germany
Posts: 3,652
#2: Apr 24 '09

re: Memory concern with a Class in JavaScript.


as far as I understand this.someMethod() is stored as part of the constructor so every created object gets its own copy (thus using more memory), while prototype.someMethod is inherited.
dmjpro's Avatar
Lives Here
 
Join Date: Jan 2007
Location: India (West-Bengal)
Posts: 2,451
#3: Apr 24 '09

re: Memory concern with a Class in JavaScript.


Quote:

Originally Posted by Dormilich View Post

as far as I understand this.someMethod() is stored as part of the constructor so every created object gets its own copy (thus using more memory), while prototype.someMethod is inherited.

That's what i heard that but had some confusions .. That means the function definitions have only one copy, only if "this.someMethods" used then the reference copy goes to every instance ;)
In inheritance, each instance has their own copy ;)
dmjpro's Avatar
Lives Here
 
Join Date: Jan 2007
Location: India (West-Bengal)
Posts: 2,451
#4: Apr 24 '09

re: Memory concern with a Class in JavaScript.


Quote:

Originally Posted by dmjpro View Post

That's what i heard that but had some confusions .. That means the function definitions have only one copy, only if "this.someMethods" used then the reference copy goes to every instance ;)
In inheritance, each instance has their own copy ;)

Sorry i might have wrong conceptions .. actually let me first see the full one then it would be better to comment through ..Really interesting video. Behind which i have been running , that i am getting here. Basically it was too boring to read the full manual ;)

Actually lastly what i came to know that whole function body copied with each Instance. And prototype is a hidden reference with each Instance which holds shared properties.

Actually what i seen so interesting ..........
Have a loot this

Points 4 and 5 i didn't understand, please make me understand ;)

If anyone wants to see this video, then please click it ..
Dormilich's Avatar
Moderator
 
Join Date: Aug 2008
Location: Leipzig, Germany
Posts: 3,652
#5: Apr 24 '09

re: Memory concern with a Class in JavaScript.


Quote:

Originally Posted by dmjpro View Post

Basically it was too boring to read the full manual

often a source for misunderstandings… that's why "reading the manual" is considered a skill.
dmjpro's Avatar
Lives Here
 
Join Date: Jan 2007
Location: India (West-Bengal)
Posts: 2,451
#6: Apr 24 '09

re: Memory concern with a Class in JavaScript.


The properties' values are not copied at creation time. They are dynamically looked up at runtime.

What does it mean ?

1 Sec .. Does it mean the properties associates with the ClassName.prototype?
Reply