Solved

JavaScript return property vs prototype property

Posted on 2014-04-10
7
113 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
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Although it can be difficult to imagine, someday your child will have a career of his or her own. He or she will likely start a family, buy a home and start having their own children. So, while being a kid is still extremely important, it’s also …
This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
This video teaches users how to migrate an existing Wordpress website to a new domain.
The viewer will learn how to count occurrences of each item in an array.

911 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now