What is the difference between assigning a function via this vs. prototype? [duplicate]

2024/2/27 7:33:35

Possible Duplicate:
Use of 'prototype' vs. 'this' in Javascript?

I am confused with these two type of adding a method to a function. Let me explain with an example.

var foo = function(){this.bar = function(){alert('I am a method')}
}foo.prototype.baz = function(){alert('I am another method')}var car = new foo();

Now at this point we can use baz and bar methods for car. Well, but what is the difference between them. What is the nuance adding a method to function's prototype or it's constructor.



Functions assigned to the prototype will be shared by all instances; functions assigned in the constructor will have a separate function object per instance.

Also, functions assign in the constructor can use the constructor's variables and parameters.

For example:

var foo = function(param){this.bar = function() { alert('I can see a parameter: ' + param); };
}foo.prototype.baz = function() { alert('I can't see foo's parameters'); };var car = new foo("Hi there!");

Related Q&A

In Jquery How to handle paste? [duplicate]

This question already has answers here:Closed 12 years ago.Possible Duplicates:How do you handle oncut, oncopy, and onpaste in jQuery?jQuery catch paste input I have a textarea, on paste to that text…

JavaScript Function Queue

I have a ton of functions that need to run in succession, but not before the other has completed. What I need is a way to queue these functions to run only after the previous function was successfully …

how to find the latest selected item of a multiple select tag using jquery?

I have a <select multiple="multiple" id="multi"> in my website, and I have a script in jquery that has this function:$("#multi").on("change",function(){}) …

How do I change a glyphicon upon clicking it in Bootstrap 3.2?

I want to have an arrow pointing to the right to allow the user to expand the sidebar, and then change that glyphicon to point to the left. That way, it points to the left so that they understand how t…

Angular $setPristine() not working

Im trying to use Angulars built-in form functions, specifically setPristine() to clear the form on user submit. My controller has access to $scope.newForm (my form) with all of its methods, but running…

Laravel and google maps : foreach latitude/longitude show marker or map

I have a table locations with a latitude and longitude field.For each location I want a new map (or better a new marker) that uses the latitude and longitude from the table locations to display a city …

Dynamics 356 v9 - use executionContext.getFormContext() with HTML web resource

Any ideas how we can we use executionContext.getFormContext() with an HTML web resource? only method that seems to work is parent.Xrm.Page which is now deprecated.

Using this instead of scope with AngularJS controllers and directives

I was recently reading John Papas opinionated AngularJS style guide and noticed his convention on controllers:/* recommended */ function Customer () {var vm = this;vm.name = {};vm.sendMessage = functio…

Whats the difference between using this and the object name for reference inside the object?

If I have the following code:var obj = {x: 34,init: function() {alert(this.x);alert(obj.x)}};Both alerts displays 34. But what is the difference, and is one better than the other?http://jsfiddle.net/4…

How to modify / add to Cookie in JMeter?

Im very new to JMeter and need your help on how to modify a cookie.Here is the scenario: Im testing an assessment/test taking website that offers multiple answers to questions. When a user makes his se…