0

my English is not so good, but i try to explain my self clear. I just started learn JS objects and stumbled upon the problem that i can't understand.

I got a simple object like

var cars = {
   doors: 4,
   wheels: 4
}

and when i trying to create an object like this:

   var Opel = new car()

I got an error Uncaught TypeError: object is not a function

And when i do it like this :

Opel = Object.create(cars)

all going fine.

And when i writing a an Object like this :

 function cars() {}

a method to declare the object with new, work correctly. I can't understand what the difference between thous two type of writing the objects.

Thanks for advice.

Victorino
  • 1,623
  • 11
  • 22
  • Please keep reading the tutorials on JS, it will come to you. Meanwhile, SO is not really designed to be a replacement for learning a language on your own. –  Aug 23 '14 at 19:24
  • I recommend to read the MDN JavaScript Guide: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Working_with_Objects , which explains all the different ways of creating an object. – Felix Kling Aug 23 '14 at 19:25
  • I'm understand, that when i write it like a function it is constructor.it is different from PHP, but why without it gives an error when i declaring it with new? because it's don't have constructor? – Victorino Aug 23 '14 at 19:26
  • Maybe the following answer can help. It answers what a constructor function does and what prototype does: http://stackoverflow.com/a/16063711/1641941 – HMR Aug 23 '14 at 19:26
  • You can only call **functions** with `new`. As the error tells you, `car` is not a function, hence you can't call it with `new`. – Felix Kling Aug 23 '14 at 19:31

1 Answers1

1

You didn't understand prototyping correct.

To define a class you create a simple function, like:

function Car(){
   this.doors = 4; //For instance, not really necessary 
}

You can set properties on this in the function.

Then you define a prototype, every object of class "Car" will have all this properties (and "methods"):

Car.prototype = {
   doors: 4, //we don't need to set this again if we already did in the constructor, but I'll leave if it anyway
   wheels: 4
}

Please see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Introduction_to_Object-Oriented_JavaScript for more.

idmean
  • 14,540
  • 9
  • 54
  • 83
  • thanks. one more question. when i creating a object like var foo = {} what this means? i came from PHP and in js it is different as i see – Victorino Aug 23 '14 at 19:28
  • 1
    @Viktorino You create an empty object, like `new stdClass()` in PHP or an empty associative array. In JavaScript there are no instances of a class, only objects which were created from a prototype and therefore have the object's functions (methods) and properties (instance variables) – idmean Aug 23 '14 at 19:30
  • 1
    @Viktorino `foo={doors:4}` is an instance of an object (object literal) Not sure what the equivalent in PHP is but I think it would be an array `array('doors'=>4)` If you need to create many instances with roughly the same characteristics you use a constructor function in JavaScript or a class in PHP. – HMR Aug 23 '14 at 19:31
  • aaaa.. now i understand it. thank you for help! – Victorino Aug 23 '14 at 19:32