1

enter image description herei just tried the following way of having a function assigned for a object. It works on chrome(43.0) but does not work on firefox(36.0.4)

code:

var obj = new Object();

obj.name = "Andrew";
obj.age = 20;

obj.print = function(){
  console.log( this.name );
  console.log( this.age );
}

obj.print(); // printing undefined in Firefox

I know of other ways of adding a function to an object such as Object.getPrototypeOf() and Object.defineProperty() but those are for json objects i suppose. The code sample above uses the object constructor, i want to know how to add a method to an object created with the object constructor. If its just not possible to have methods in an object created with the object constructor, let me know of that too. I know how to use JSON and get the methods within it or use call and apply, this is just for finding out if there is a way to add a method for objects using the new Object() constructor.

Thank you.

Bharat
  • 152
  • 1
  • 9
  • works fine for me. in firefox it prints out Andrew and 20 to the console – atmd Jul 07 '15 at 14:55
  • What exactly does it print out? I'm suspecting a duplicate of [Chrome/Firefox console.log always prepends a line saying undefined](http://stackoverflow.com/q/14633968/1048572) – Bergi Jul 07 '15 at 14:59
  • " It works on chrome(43.0) but does not work on firefox(36.0.4)" no, it works perfectly well in firefox, it's not giving an error, just saying that your method has no return value. it's nothing to worry about – atmd Jul 08 '15 at 07:19

2 Answers2

3

In firefox the function prints out "Andrew" and "20" as you'd expect.

It does also print out undefined but thats just because you function has no return value.

it's nothing to worry about, firefox is just letting you know, chrome isnt because it wont cause any issues.

if it something that worries you, have the function return true

obj.print = function(){
  console.log( this.name );
  console.log( this.age );
  return true;
}

it will now show true in firefox instead of undefined

Also make sure that you have Log tunred on in your dev tools log level, if Log is ruened off, you'll only see the undefined messages.

atmd
  • 7,430
  • 2
  • 33
  • 64
  • Appreciate your reply, just added an image to my question, i still get undefined in firefox. Maybe its because i'm using the default "console", maybe you're trying firebug which may be better than the default console. – Bharat Jul 07 '15 at 15:59
  • You are still getting undefined because you function still doesnt return anything. it's nothing to do with the console/firebug. it it giving you undefined because you function doe not have a return value, so when you cann it the result is undefined, but there isnt anything wrong, it's working as it should – atmd Jul 08 '15 at 07:17
  • i do understand the return value , i will get `true` instead of `undefined` if i use the return statement, that is quiet clear and obvious, i'm not getting the log for the name and age at all,**can you look at the screenshot ?** before undefined or true, what i should see is name and age. – Bharat Jul 08 '15 at 13:03
  • 1
    I see, have you checked the loggin level to make sure you have 'log' turned on? (little dropdown to the right of `logging` on the dev tools – atmd Jul 08 '15 at 13:17
  • Hey, that just fixed it, thanks a ton, so it is practically possible to have a property assigned to a function then, even if you use the `new Object()` constructor. Thank you so much. :) It was turned off but today when i tested it in a new tab it worked, cos logging was `on`, so toggled and checked it out again...I guess that was the issue, thank you :) – Bharat Jul 08 '15 at 13:21
0

Have you tried declaring the object like this? :

 var obj = {

      name : "Andrew",

      age  : "20"

    }
  • it's the `print` method call that created the output. putting this in firefox console will still give `undefined`, it's normal/expected behaviour – atmd Jul 07 '15 at 15:00
  • @Bharat This is not JSON, this is just Javascript. (an JSON is based on Javascript, which is why it looks somewhat similar) – dsh Jul 07 '15 at 18:50