Linked Questions
25 questions linked to/from Declaring javascript object method in constructor function vs. in prototype
836
votes
16
answers
133k
views
Use of 'prototype' vs. 'this' in JavaScript?
What's the difference between
var A = function () {
this.x = function () {
//do something
};
};
and
var A = function () { };
A.prototype.x = function () {
//do something
};
4
votes
2
answers
231
views
What is the point of prototypes in JavaScript? Why not add method directly to constructor? [duplicate]
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....
0
votes
1
answer
87
views
Should I declare javascript object methods in an object prototype? [duplicate]
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 ...
1
vote
2
answers
59
views
adding function in prototype versus declaring it as variable in OOP Javascript [duplicate]
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 = ...
0
votes
1
answer
77
views
whey append methods to the Object prototype instead of put them on constructor function ? [duplicate]
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 ...
336
votes
3
answers
95k
views
Advantages of using prototype, vs defining methods straight in the constructor? [duplicate]
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) {
...
14
votes
5
answers
3k
views
JavaScript OOP: method definition with or without "prototype"
Is this code,
function Person() {
function myMethod() {
alert ('hello');
}
this.method = myMethod;
}
equivalent to:
function Person() { }
Person.prototype.method2 = function(...
8
votes
4
answers
11k
views
How do I represent a return type in a Typescript interface file?
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 ...
6
votes
3
answers
14k
views
Adding listener functions to a JavaScript object
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 ...
2
votes
3
answers
3k
views
Can we assign a public method inside a object constructor ? (javascript)
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 ...
3
votes
3
answers
3k
views
Convert a large javascript file into multiple files
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 ...
0
votes
3
answers
2k
views
Javascript define a constant object property
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);
...
3
votes
5
answers
228
views
Do I always have to apply the "this" ( or "that" ) scope to all object properties in JavaScript?
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 ...
0
votes
1
answer
435
views
Difference between method in a constuctor function vs function's prototype property [duplicate]
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 ...
2
votes
4
answers
684
views
JavaScript Inherited Properties Default Value
Consider having below code
function Employee() {
this.id = "";
this.name = "";
this.gender = "";
}
function Programmer() {
this.expertise = "";
}
Programmer.prototype = new Employee(...