Link to home
Start Free TrialLog in
Avatar of itnifl
itniflFlag for Norway

asked on

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

ASKER CERTIFIED SOLUTION
Avatar of Kim Walker
Kim Walker
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of itnifl

ASKER

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?
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.
Avatar of itnifl

ASKER

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).
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.
Avatar of itnifl

ASKER

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
Avatar of itnifl

ASKER

I am assuming you are right :)