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.

3
  • works fine for me. in firefox it prints out Andrew and 20 to the console Commented Jul 7, 2015 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 Commented Jul 7, 2015 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 Commented Jul 8, 2015 at 7:19

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

5 Comments

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.
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
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.
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
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 :)
0

Have you tried declaring the object like this? :

 var obj = {

      name : "Andrew",

      age  : "20"

    }

2 Comments

it's the print method call that created the output. putting this in firefox console will still give undefined, it's normal/expected behaviour
@Bharat This is not JSON, this is just Javascript. (an JSON is based on Javascript, which is why it looks somewhat similar)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.