2

Why is it incorrect to think of variables (and methods) on the prototype as being static? I know that we use the instance to call/change them but if we change them then that change is reflected to all instances and not just the instance itself.

function Calculator() {
    // constructor defined
    this.add = function(a, b) {
    return a+b;
  }
}

Calculator.prototype.staticvar = 'hello'

c1 = new Calculator();
c2 = new Calculator();

alert(c1.staticvar + ", " + c2.staticvar) // both hello

Calculator.prototype.staticvar = "hey!"

alert(c1.staticvar + ", " + c2.staticvar) // both change to hey!

Is it the fact that by definition a static variable can be accessed directly with the class, without the need to create a instance becomes the main reason why variables/functions on a prototype object are not considered static?

noi.m
  • 3,070
  • 5
  • 34
  • 57
  • This is a misunderstanding of prototypal inheritance. The prototype *is* an instance and not a class definition. Instances automatically get anything that was set on the prototype. You can think of each new instance as a copy of the prototype object. – Patrick Goley Mar 27 '17 at 22:25
  • 1
    *"I know that we use the instance to call/change them but if we change them then that change is reflected to all instances and not just the instance itself."* If you use an instance to change it, the change is not reflected on all other instances. In your code, you're not changing an instance; you're changing the shared `.prototype`. –  Mar 27 '17 at 22:27
  • Hey guys, it could very well be "a misunderstanding of prototypal inheritance". If possible, please create answers so i can mark them as correct. – noi.m Mar 27 '17 at 22:30
  • See [How does JavaScript .prototype work?](http://stackoverflow.com/questions/572897/how-does-javascript-prototype-work) – guest271314 Mar 27 '17 at 22:33

2 Answers2

0

They're not static, they're inherited. Static would imply it's accessible via Calculator.staticvar, not calculatorInstance.staticvar. So to answer your question, yes, it has to do with the fact that you don't need to create an instance variable.

In fact, in ES6 classes, this is the case when using the static specifier on member methods:

class Calculator {
  static get staticvar(){ return this._staticvar; }
  static set staticvar(value){ return this._staticvar = value; }
}

// "private" property, signified by leading underscore
Calculator._staticvar = 'hello';

console.log(Calculator.staticvar);

Calculator.staticvar = 'hey!';

console.log(Calculator.staticvar);
Patrick Roberts
  • 49,224
  • 10
  • 102
  • 153
0

Javascript supports prototypical inheritance unlike other programming languages like Java. A function prototype is shared among all instances of the function.

I have modified the code to understand difference.

function Calculator() {
    // constructor defined
    this.add = function(a, b) {
        return a+b;
    }
}

Calculator.prototype.staticvar = 'hello';

c1 = new Calculator();
c2 = new Calculator();

alert(c1.staticvar + ", " + c2.staticvar) // both hello

c1.staticvar = "hey!";

alert(c1.staticvar + ", " + c2.staticvar) // alerts hey, hello

So instance c1 has its own property staticvar. And c2 doesn't have its own property(staticvar). Hence c2.staticvar referes to its prototype object.

So, changing a property on instance doesn't change property value of other instances.