Creating "static" members in a javascript object [duplicate] - javascript

This question already has answers here:
Static variables in JavaScript
(43 answers)
Closed 8 years ago.
Is there a way to create "static" members in a JS Object?
function Person(){
}
Person.prototype.age = null;
Person.prototype.gender = null;
I would like to add personsCount as a static member, is that possible?

Sure, just add Person.personsCount without the prototype

Common practise is to make such "static members" properties of the constructor function itself:
function Person() {
Person.count++;
}
Person.count = 0;

Related

JavaScript Prototype Functions [duplicate]

This question already has answers here:
Use of 'prototype' vs. 'this' in JavaScript?
(15 answers)
Closed 6 years ago.
What is the difference between these functions? And what are the advantages and disadvantages?
let Test = function(name) {
this.name = name;
this.complete = function() {
console.log(`completing task${this.name}`)
}
}
and
let Test = function(name) {
this.name = name;
}
Test.prototype.complete = function() {
console.log(`completing task${this.name}`)
}
The difference is that each object you will create with the first function would have it's separate instance of complete function. While each object you will create with the second function would share the same complete function, which can be found at the prototype of the object you create using Test.
So if you create 100 objects using the first function, all these objects would have a different reference in memory for the complete function. While in the second case you would have only one, since all these objects would find the complete function on their prototype.

Is there a useful difference between defining a method in an object contructor and appending it to the constructor's prototype object? [duplicate]

This question already has answers here:
Use of 'prototype' vs. 'this' in JavaScript?
(15 answers)
Closed 7 years ago.
Consider this code example:
function Person(config) {
this.name = config.name;
this.age = config.age;
}
Person.prototype.getAge = function() {
return this.age;
};
var tilo = new Person({name:"Tilo", age:23 });
console.log(tilo.getAge());
Rather than defining getName() as an appendage to the constructor's prototype property, it seems to me it could be defined within the constructor to achieve the same thing? Is there any useful difference? In other words, does doing it one way or the other have any particular implementation advantage?
The constructor will create a new instance of that function with each invocation. Putting it on the prototype will save memory.

Why we use "prototype" property to add methods to constructor instances? [duplicate]

This question already has answers here:
Use of 'prototype' vs. 'this' in JavaScript?
(15 answers)
Closed 7 years ago.
I am creating a JavaScript constructor. Consider this example:
function Student(name)
{
this.name = name;
}
Student.prototype.printName = function(){
console.log(this.name);
}
var s = new Student("Eden");
s.printName();
This above code works all well. But I can write the same this way also:
function Student(name)
{
this.name = name;
this.printName = function(){
console.log(this.name);
}
}
var s = new Student("Eden");
s.printName();
I feel the second method is correct way because printName should be own property of new object not an inherited property. Adding to prototype makes it an inherited property.
Why do developer prefer the first method?
By attaching functions directly to this in a constructor, that means every instance of Student has it's own copy of printName. That isn't very efficient. By attaching it to the prototype chain of Student, there is only one copy of the printName function and any instance of Student has it in it's prototype chain.
While there are other differences in those two approaches for attaching function to objects, that is the simplest and easiest to remember.
If you want the full dictionary of reasons how the two differ, please refer to this answer to an extremely similar question.

How to instantiate class dynamically [duplicate]

This question already has answers here:
How to execute a JavaScript function when I have its name as a string
(36 answers)
Closed 8 years ago.
I have an array with names of constructors var arr = ['Class1', 'Class2', 'Class3'].
function Class1() {
this.name = 'class1';
}
Is it possible to dynamically create instances of these classes? I mean something like
var class1Object = new arr[0]();
I tried that but it isn't working (Uncaught TypeError: string is not a function).
functions defined in "global" scope are actually created on the window object, so you can do this (as long as the code is in the head of the page, and not scoped to something specific):
function Class1(){
this.name = 'class1';
}
var className = "Class1";
var c1 = new window[className]();
Live example: http://jsfiddle.net/vdf4W/
Previously answered in this post (Google: Dynamic Instantiation In JavaScript)
Dynamic Instantiation In JavaScript

Possible to override a base classes privileged function? [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Is there an equivalent of PHP's 'parent' with javascript prototypal inheritance?
Override a base class function
I have a base class/prototype & a child class/prototype in Javascript.
Is it possible to make the child class override a base classes privileged function?
I know I am trying to treat javascript as an OO language which its not but you never know this maybe possible?
If its possible could you give an example how I do this?
function baseClass()
{
this.privFunct() {}
}
function childClass()
{
this.privFunct()
{
var baseFunct = baseClass.prototype.privFunct;
this.baseFunct();
// no do some object specific actions here
}
}

Categories

Resources