Solved

JavaScript return property vs prototype property

Posted on 2014-04-10
7
116 Views
Last Modified: 2014-04-15
What is the difference?

function() classname {
  return {
	parseValue: function (input) {
		{ return _private(input); }
	}
  }
}

Open in new window

VS
classname.prototype.parseValue = function(input) {
	return _private(input);
}

Open in new window

0
Comment
Question by:itnifl
  • 4
  • 3
7 Comments
 
LVL 22

Accepted Solution

by:
Kim Walker earned 400 total points
ID: 39992329
In the first case you're returning an object. In the second your returning a value.
0
 
LVL 2

Author Comment

by:itnifl
ID: 39992399
I am returning an object because of the brackets surrounding "return _private(input);" in the first example? But both examples create the property parseValue? When would I want to create the property parseValue like in the first example, and when would I want to do it like in the second example?
0
 
LVL 22

Expert Comment

by:Kim Walker
ID: 39992458
I don't think the first example is even possible in Javascript. Javascript doesn't really have classes, but javascript objects are very similar to classes.

Are you confusing javascript with Java? These are two entirely separate programs. It's unfortunate that Netscape decided to use the name "javascript." Unfortunately, my Java knowledge is very rudimentary though I know my way around javascript very well.
0
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 
LVL 2

Author Comment

by:itnifl
ID: 39993659
Hello xmediaman, and thank you for your reply. I am aware of the difference between Java and JavasScript. This mixup that people often make is really a pain in the ass, I am guessing you are thinking the same.

Here I have written a small fiddle that exemplifies object oriented JavaScript:
http://jsfiddle.net/bMM5J/

Read the comments and try it out if you like.

I am looking for if there are any real differences in declaring attributes of an object in the first example shown in my question or the second(classname.prototype.parseValue).
0
 
LVL 22

Expert Comment

by:Kim Walker
ID: 39993930
Sorry, I actually got hung up on line one of your first example where you have the parentheses after "function" and not after the "classname". Unfortunately, I'm not very adept in the difference between these two declarations. But I would think that the first would be problematic in that it must create and return an anonymous object every time it's executed.
0
 
LVL 2

Author Comment

by:itnifl
ID: 39993984
Yes, you may be right about that. I am not used to doing it this way as the first example shows and would like to understand it better. I have seen example code with this way of writing. The parentheses after the function keyword is a mistake from my side. It was supposed to be after the function name :p
0
 
LVL 2

Author Closing Comment

by:itnifl
ID: 40001687
I am assuming you are right :)
0

Featured Post

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Because your company can’t afford for you to make SEO mistakes, you’ll want to ensure you’re taking the right steps each and every time you post a new piece of content. This list of optimization do’s and don’ts can help you become an SEO wizard.
Color can increase conversions, create feelings of warmth or even incite people to get behind a cause. If you want your website to really impact site visitors, then it is vital to consider the impact color has on them.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
Any person in technology especially those working for big companies should at least know about the basics of web accessibility. Believe it or not there are even laws in place that require businesses to provide such means for the disabled and aging p…

803 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question