admin管理员组文章数量:1435859
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 defineGreetr.init.prototype
. – Louay Alakkad Commented Jan 12, 2016 at 8:48
4 Answers
Reset to default 4name 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;
}
本文标签: javascriptquotobject Objectquot of type StringStack Overflow
版权声明:本文标题:javascript - "[object Object]" of type String - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745645661a2668118.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论