最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

javascript - "[object Object]" of type String - Stack Overflow

matteradmin6PV0评论

I am taking a course on Udemy "Javascript: Understanding the Weird Parts", and ran into a problem that won't let me move forward. I am building a small js framework. That has a simple structure:

(function (global, $){
   var Greetr = function(firstName, lastName, language){
       return new Greetr.init(firstName, lastName, language);
   }

   Greetr.prototype = {};

   Greetr.init = function(firstName, lastName, language){

      var self = this;

      self.firstName = firstName || "<first name>";

      self.lastName = lastName || "<last name>";

      self.language = language || "en";

   }

   Greetr.init.prototype = Greetr.prototype;

   global.Greetr = global.G$ = Greetr;

}(window,jQuery));

When I try to use my framework, I get an unexpected result:

var name = Greetr("John", "Doe");
console.log(name);

Outputs to the console: "[object Object]", of the strin type.

Am I missing something? Can you, please, help me figure out the solution?

Thank you in advance!

UPDATE #1 I am expecting an object, so that I can browse it in the Chrome's console, just like if you type 'window' in it.

UPDATE #2 Surprisingly, the error was in naming my object 'name':

var name = G$("John", "Doe");

Once I renamed it to something else, it worked just fine!

I am taking a course on Udemy "Javascript: Understanding the Weird Parts", and ran into a problem that won't let me move forward. I am building a small js framework. That has a simple structure:

(function (global, $){
   var Greetr = function(firstName, lastName, language){
       return new Greetr.init(firstName, lastName, language);
   }

   Greetr.prototype = {};

   Greetr.init = function(firstName, lastName, language){

      var self = this;

      self.firstName = firstName || "<first name>";

      self.lastName = lastName || "<last name>";

      self.language = language || "en";

   }

   Greetr.init.prototype = Greetr.prototype;

   global.Greetr = global.G$ = Greetr;

}(window,jQuery));

When I try to use my framework, I get an unexpected result:

var name = Greetr("John", "Doe");
console.log(name);

Outputs to the console: "[object Object]", of the strin type.

Am I missing something? Can you, please, help me figure out the solution?

Thank you in advance!

UPDATE #1 I am expecting an object, so that I can browse it in the Chrome's console, just like if you type 'window' in it.

UPDATE #2 Surprisingly, the error was in naming my object 'name':

var name = G$("John", "Doe");

Once I renamed it to something else, it worked just fine!

Share Improve this question edited Jan 12, 2016 at 23:11 sheriff_paul asked Jan 12, 2016 at 8:37 sheriff_paulsheriff_paul 1,0853 gold badges15 silver badges31 bronze badges 1
  • Add a toString function. Greetr.init.prototype.toString = function() {return this.firstName + ' ' + this.lastName; };. Btw, Greetr.prototype is useless here and you only need to define Greetr.init.prototype. – Louay Alakkad Commented Jan 12, 2016 at 8:48
Add a ment  | 

4 Answers 4

Reset to default 4

name is a Javascript object, so you cannot echo it as is, because it is not a scalar variable. An object may have a large structure and parent-child references. If you want to echo an object values, you can either implement a toString() method in your class which build a string you can use,

console.log (name.toString ()) ;

or use the json format.

console.log (JSON.stringify (name)) ;

you can log an object with this :

function clog (data){
  console.log(JSON.stringify(data, undefined, 2));
}
clog(name);

name is an Object that contains firstName, lastName, language. name.firstName should give you value of firstname "John".

Name is an object so to get a proper output you got to override toString:

// toString override added to prototype of your class
yourClass.prototype.toString = function()
{
   // return "[object yourclass]"; this is normally what it will return 
   return this.firstName + ' ' + this.lastName;
}
Post a comment

comment list (0)

  1. No comments so far