0

I made my object a constructor function cause I did not want to put my code in another file or my object on top. Why do objects have to be initialized before use if a function does not and a function is considered an object ?

var foo_f = new Foo_methods; 
var foo_o = Object.create(null);
foo_o = Object.create(foobar_obj);

foo_f.name() //Works, functions can be initialized anywhere. 
foo_o.name() // Error you have to put the object on top

function Foo_methods(){

  this.name = function(){ console.log("I am foo in Foo_methods")}
}

//If I put this on top or in another file it works. If I don't I get an error
//Type Error: Object prototype may only be an Object or null: undefined

var foobar_obj = {

         name:function(){ console.log("I am foo in foobar_obj")}
}
ritchie
  • 405
  • 1
  • 4
  • 12
  • 2
    It’s called function and variable hoisting. – Sebastian Simon Jan 22 '16 at 23:17
  • 2
    when foo_o = Object.create(foobar_obj), is being executed, it does not know what foobar_obj is, because it has not been declared yet. any var myVarialbes should be placed at the top of the page – Ji_in_coding Jan 22 '16 at 23:17
  • Aww ok lol, so simple but true. A work around if I wanted to use an object would have to make a function on the bottom initialize_obj(/*object and object code here */ ); – ritchie Jan 22 '16 at 23:29

0 Answers0