Solved

JavaScript return property vs prototype property

Posted on 2014-04-10
7
109 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 21

Accepted Solution

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

Author Comment

by:itnifl
Comment Utility
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 21

Expert Comment

by:Kim Walker
Comment Utility
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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 2

Author Comment

by:itnifl
Comment Utility
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 21

Expert Comment

by:Kim Walker
Comment Utility
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
Comment Utility
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
Comment Utility
I am assuming you are right :)
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
Any business that wants to seriously grow needs to keep the needs and desires of an international audience of their websites in mind. Making a website friendly to international users isn’t prohibitively expensive and can provide an incredible return…
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…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)

771 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

12 Experts available now in Live!

Get 1:1 Help Now