A
A
Alexander Korolev2016-02-18 10:21:58
JavaScript
Alexander Korolev, 2016-02-18 10:21:58

Creating objects in js using prototype?

Good afternoon.
I often come across this way of creating objects in JS:

function Obj = {
  this.someVar = 1;
  this.someVar2 = 2;
}
Obj.prototype = {
 fun1: function() {},
 fun2: function() {}
}

Why is the assignment of functions through a prototype used?
I thought that the prototype is needed to inherit functions from the ancestor object?
After all, you can simply set the functions:
function Obj = {
   this.fun = function () {};
}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
G
GreatRash, 2016-02-18
@GreatRash

Well, first of all, the code you wrote below won't work...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question