Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 124
  • Last Modified:

JavaScript return property vs prototype property

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
itnifl
Asked:
itnifl
  • 4
  • 3
1 Solution
 
Kim WalkerWeb Programmer/TechnicianCommented:
In the first case you're returning an object. In the second your returning a value.
0
 
itniflAuthor Commented:
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
 
Kim WalkerWeb Programmer/TechnicianCommented:
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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
itniflAuthor Commented:
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
 
Kim WalkerWeb Programmer/TechnicianCommented:
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
 
itniflAuthor Commented:
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
 
itniflAuthor Commented:
I am assuming you are right :)
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

  • 4
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now