0

i am a newbie to Js OOp. I was reading somewhere that Prototypes are used in OOP in JS Ok here is an example

function Dog(name) {
    this.name = name;
    this.age = age;
    this.bark = function() {
        console.log("Woof");
    }
}

Now my question is why can't we just create objects like this

Dog doberman = new Dog;

This creates a new Dog object.

But through prototypes we do something like this

var doberman = Object.create(dog);

I read that the functions can be separated and through prototype they can be added to the object. Please help me. I am confused..

Well the question simply is Why use Object.prototype for methods?

Seth
  • 10,198
  • 10
  • 45
  • 68
Cloudboy22
  • 1,496
  • 5
  • 21
  • 39
  • Prototype allow you to assure that any object of that class will have (at least initially) that property (i.e. variable, method). – MaxZoom Oct 21 '15 at 04:04
  • 1
    JavaScript is not typed so you can't use `Dog doberman = new Dog()` but you most certainly can use `var doberman = new Dog()` – Phil Oct 21 '15 at 04:05
  • For `Object.create`, I suggest you read the docs ~ https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/create – Phil Oct 21 '15 at 04:05
  • 1
    It's OK to hit the space bar when you're programming in JavaScript. – Pointy Oct 21 '15 at 04:06
  • 2
    *Note* prototypes can also help with separating methods of an object, such that they are not recreated with each instance. – Spencer Wieczorek Oct 21 '15 at 04:06
  • 1
    I suggest to read https://developer.mozilla.org/en-US/docs/Web/JavaScript/Introduction_to_Object-Oriented_JavaScript and https://github.com/getify/You-Dont-Know-JS/blob/master/this%20&%20object%20prototypes/ch5.md – Felix Kling Oct 21 '15 at 04:06
  • @SpencerWieczorek hey thanks can you please tell me how to create an object with a prototype which contains methods? – Cloudboy22 Oct 21 '15 at 04:11
  • @MarcAndreJiacarrini Sure, here is an [example with the `bark` method](http://jsfiddle.net/5veucgqq/). You would just do `objectName.prototype.method = function(){ ... }` in general. – Spencer Wieczorek Oct 21 '15 at 04:15
  • @SpencerWieczorek ok but i i create say 20 objects from a constructor function and have another function say bark then i have to do object.prototype.method for all those 20 methods right? – Cloudboy22 Oct 21 '15 at 04:19
  • @MarcAndreJiacarrini The prototype is attached to the "class" in this case, you don't need to redo it for each instance. – Spencer Wieczorek Oct 21 '15 at 04:21
  • @SpencerWieczorek answer me (i mena not comment) and i will accept it.. thanks – Cloudboy22 Oct 21 '15 at 04:38
  • @MarcAndreJiacarrini Actually since this was marked as a duplicate it cannot accept any more answers. – Spencer Wieczorek Oct 21 '15 at 04:40

1 Answers1

2

Now my question is why can't we just create objects like this: Dog dog = new Dog;

You can, actually. Your Dog function also serves as a "constructor-function", like so:

var doberman = new Dog( "Fenton" );

I appreciate this is confusing because JavaScript offers many different ways of doing (essentially) the same thing, for example:

var doberman = { name: "Fenton", age: 5, bark: function() { return "woof"; } }

(though my first example creates a new object instance that uses Dog as a prototype (a kind of inheritance), whereas my second example creates a new object instance with only instance-level properties that are not inherited, though thanks to JavaScript's duck-typing there are very few times where the difference matters (though in high-performance JavaScript always prefer prototype constructors over instance-level properties).

Dai
  • 141,631
  • 28
  • 261
  • 374
  • You mean We can also do OOP in Js ithout prototypes?? Well it will effect performance right cause as they are not recreated with every instance?? – Cloudboy22 Oct 21 '15 at 04:09
  • @MarcAndreJiacarrini Yes you can, although using prototypes on methods can save you memory for each instance. If you don't have too many instances simply not using a prototype would not affect you very much. – Spencer Wieczorek Oct 21 '15 at 04:24