0

Lets take a look at these two examples:

first:

function Cars (name, doors) {
    this.name = name;
    this.doors = doors;
    function getInfo() {
        return this.name + " " + this.doors;
    };
}

second:

function Cars (name, doors) {
        this.name = name;
        this.doors = doors;
    }

Cars.prototype.getInfo = function() {
    return this.name + " " + this.doors;
}

I know that in first example saving the getInfo() method as object instance would waste memory this.getInfo() = function() { this.cars + " " + this.doors;} and it would be inherited, but is there a difference if the method would be defined as a non-object property - like I did in the first example and not as an object instance (I'm missing words here), besides that it would be statically defined?

What is the difference between

this.getInfo() = function() { return this.cars + " " + this.doors; }

and

function getInfo() {
    return this.cars + " " + this.doors;
}

Performance and all other differences?

Also, is there a way to access private local variables with obj.prototype?

Thanks.

tholo
  • 511
  • 3
  • 8
  • 19

0 Answers0