Answer the question
In order to leave comments, you need to log in
Difference of Complex.add from Complex.prototype.add?
Good afternoon community!
I'm reading the JavaScript book (David Flanagan, 5th edition) and in chapter 9 (9.3.6 chapter, 176 pages) there is an example of how to create a "class".
Help me understand the difference between .add method and .add prototype
function Complex(real, imaginary)
{this.x = real; this.y = imaginary;};
Complex.prototype.add = function(that)
{return new Complex(this.x + that.x, this.y + that.y); };
Complex.add = function(a, b)
{return new Complex(a.x + b.x, a.y + b.y); };
Answer the question
In order to leave comments, you need to log in
var complex1 = new Complex(1,2);
var complex2 = new Complex(3,4);
var c3 = Complex.add(complex1, complex2);//сработает Complex.add = function(a, b)
var c4 = complex1.add(complex2);//сработает Complex.prototype.add = function(that)
var c5 = complex1.add(complex1, complex2);//сработает Complex.prototype.add = function(that) но параметр 2 просто проигнорируется и получим complex1 + complex1
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question