Linked Questions

836 votes
16 answers
133k views

What's the difference between var A = function () { this.x = function () { //do something }; }; and var A = function () { }; A.prototype.x = function () { //do something };
sw234's user avatar
  • 8,369
4 votes
2 answers
231 views

So I am going over a few lessons over JavaScript at CodeAcademy, and I ran into this section regarding prototypes. A bit from the code: function Dog(breed){ this.breed = breed; }; Dog.prototype....
user1768884's user avatar
  • 1,047
0 votes
1 answer
87 views

Everything I have read seems to favor declaring methods of object constructor functions in a prototype declarations instead of putting the method straight into the initial constructor. function ...
mikeLspohn's user avatar
1 vote
2 answers
59 views

What is the advantage or different adding a function onto the prototype object versus simply declaring it on the constructor's variable? this is the same, this is how we call Person const James = ...
Casa Lim's user avatar
  • 385
0 votes
1 answer
77 views

Here is my problem, i have one object Car & theirs properties, i defined a method inside the object. but i think it is recommended to append this method to the object protytype, but why? what are ...
prakashapkota's user avatar
336 votes
3 answers
95k views

I am wondering if there are any advantages of using any of these over the other, and which way should I go? Constructor approach: var Class = function () { this.calc = function (a, b) { ...
Leo's user avatar
  • 3,872
14 votes
5 answers
3k views

Is this code, function Person() { function myMethod() { alert ('hello'); } this.method = myMethod; } equivalent to: function Person() { } Person.prototype.method2 = function(...
Jean-Philippe Martin's user avatar
8 votes
4 answers
11k views

What is the difference between the following code: changeName(): ng.IPromise<any>; and changeName: () => ng.IPromise<any>; I understand one is a return type but I am confused about ...
user avatar
6 votes
3 answers
14k views

I have the following code which defines a Car. Each Car has a color, along with a setColor(color) function. I want to add listener functions which are called whenever setColor(color) is called, and I ...
Andrew's user avatar
  • 143
2 votes
3 answers
3k views

I am reading about prototypes in javascript, In a article http://phrogz.net/js/classes/OOPinJS.html I read that we can not assign public methods inside a object constructor in javascript ? How ...
Anshul's user avatar
  • 9,510
3 votes
3 answers
3k views

My question: How would one best go about breaking a large, monolithic javascript object literal into multiple, discrete, files? I have a single javascript file that consists of an object literal with ...
rg88's user avatar
  • 21k
0 votes
3 answers
2k views

I wanted to have an object field that is immutable. I've tried: const a = 1; var o = { x: 'This is mutable', y: a // This should not be mutable. }; o.y = 'Changed'; console.log(o); ...
F.Almeida's user avatar
  • 413
3 votes
5 answers
228 views

I'm trying to write an application in JavaScript, which I've been using for bits of scripting in web pages for years, and I find my understanding of scope and object orientation is coming up somewhat ...
glenatron's user avatar
  • 11.4k
0 votes
1 answer
435 views

Possible Duplicate: Advantages of using prototype, vs defining methods straight in the constructor? I'm trying to get a grip on the prototype property in JavaScript but I'm having trouble. I've ...
Nealbo's user avatar
  • 517
2 votes
4 answers
684 views

Consider having below code function Employee() { this.id = ""; this.name = ""; this.gender = ""; } function Programmer() { this.expertise = ""; } Programmer.prototype = new Employee(...
ch0eb1t's user avatar
  • 103

15 30 50 per page