0

I am practicing Javascript object function. Supposed I have firstName and lastName as two arguments of my function.I want to display like this {"firstName":"tim","lastName":doe} . Here is my code but it printed out undefined. Any idea? Thank you!

function myFunction(firstName, lastName) {
  this.name1 = firstName;
  this.name2 = lastName;
}
var obj = new myFunction();

console.log(myFunction('tim', 'doe'));
2
  • 1
    you should log obj which is the instance of your object Commented Mar 20, 2015 at 1:37
  • 1
    FYI, if you run this code in strict mode, it will throw an exception. Because you didn't call myFunction() using the new operator, in strict mode, this will be set to undefined and this.name1 = firstName; will throw an exception. Non-strict mode doesn't throw an error because this will point to the window object, but it does not do what you want it to do - thus why we have strict mode in the first place to keep you from writing and executing bad/wrong code. Commented Mar 20, 2015 at 1:40

5 Answers 5

2

Try this:

console.log(new myFunction('tim', 'doe'));

Or this:

console.log(obj);
Sign up to request clarification or add additional context in comments.

2 Comments

Also note that in obj, first name and last name will be null, as no parameters were passed in that constructor call.
why do I get myFunction (instance), which i don't expect it. Any idea? Thank you
2

You can try this

function myFunction(firstName, lastName) {
  this.name1 = firstName;
  this.name2 = lastName;
}
var obj = new myFunction('tim', 'doe');

console.log(obj);

You can see this documentation JavaScript Constructors

2 Comments

why do I get myFunction (instance), which i don't expect it. Any idea? Thank you
Your function is a mold of the a new object, but you will get instance of the constructor. myFunction -> mold obj -> instance So you can manages is the object, but not the mold this is just a definition
2

This kind of function called constructor, and you shouldn't call it directly. You have to use it with new.

console.log(new myFunction('tim', 'doe'));

This will print the result as you expect.

To distinguish the constructors from normal functions, it's better to name it begin with capital letter, like this:

function MyFunction(...) {...}

Comments

1

the undefined you receive is from the function not having a return value, see this post regarding that:Simple function returning 'undefined' value

to get the result you want...

function myFunction(firstName, lastName) {
    this.name1 = firstName;
    this.name2 = lastName;
}
var obj = new myFunction('tim', 'doe');

console.log(obj);

Comments

1

Let's explore what this line does: console.log(myFunction('tim', 'doe'));

This part: myFunction('tim', 'doe') executes myFunction as a function. Since myFunction does not have a return operator, it's return value is 'undefined' which is javascript's way of saying it doesn't exist. Thus, the word 'undefined' is printed on the console.

Additional tips: Try adding this line: console.log(typeof myFunction);

This should print 'function'. (May the 'typeof' operator become your best friend)

Try adding a return line as the last line of myFunctions such as:

return 'First name: ' + firstName + " Last name: " + lastName;

However, at this point the 'var obj = new myFunction();' line is unused.

Try adding another line: console.log(typeof obj);

This should print 'object' which means that 'obj' is just that - an object.

Here is a complete example you can play with:

function myFunction(firstName, lastName) {
  this.name1 = firstName;
  this.name2 = lastName;
  this.getNames = function() {
      return 'First name: ' + firstName + " Last name: " + lastName;
  }
  console.log("This executes once upon instatiation (the line with var obj = new ...)");
  return "Return value";
}

var obj = new myFunction('tim', 'doe');

console.log(typeof myFunction);
console.log(typeof obj);
console.log(obj.getNames());

Let me know if any of the above needs clarification. Good luck ...

BTW, this is what the output should look like on the console:

This executes once upon instatiation (the line with var obj = new ...)
script.js:14 function
script.js:15 object
script.js:16 First name: tim Last name: doe

2 Comments

i like your line of thought, but typeof is not a function but an assertion so it is called console.log(typeof varname); not console.log(typeof(varname));
Good point Traveling_Monk. Can you tell I dwell in C-dom most of the time? :)

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.