I try do improve my JavaScript skills. I don't understand why (5) works and (2) returns error. Isn't the same?
a.fn2() //OK
var A = function () {
this.fn = function () { alert(3); }
}
A.prototype = {
fn2: function () { alert(4); }
};
var B =
{
fn: function () { alert(1); }
}
B.prototype = {
fn2: function () { alert(2); }
};
a
is an instance of the A
class, where as B
is the class itself. Since fn2
is not defined as static function, it will only be available to an instance of class B
as opposed to the class B
itself.
If you wanted to use B
directly, you could use:
new B().fn2()
if you define B
as a function()
Alternatively, you could define fn2
the same way you have defined fn
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With