By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
449,002 Members | 1,261 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 449,002 IT Pros & Developers. It's quick & easy.

Memory concern with a Class in JavaScript.

dmjpro
100+
P: 2,476
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? ;)
Apr 24 '09 #1
Share this Question
Share on Google+
5 Replies


Dormilich
Expert Mod 5K+
P: 8,639
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.
Apr 24 '09 #2

dmjpro
100+
P: 2,476
@Dormilich
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 ;)
Apr 24 '09 #3

dmjpro
100+
P: 2,476
@dmjpro
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 ..
Apr 24 '09 #4

Dormilich
Expert Mod 5K+
P: 8,639
@dmjpro
often a source for misunderstandings… that's why "reading the manual" is considered a skill.
Apr 24 '09 #5

dmjpro
100+
P: 2,476
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?
Apr 24 '09 #6

Post your reply

Sign in to post your reply or Sign up for a free account.