JavaScript变量和相同的变量也是一个对象 [英] JavaScript variable and the same variable also be an object
本文介绍了JavaScript变量和相同的变量也是一个对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有点新的JavaScript和我有一个问题。
I am somewhat new to JavaScript and I have a question.
我知道你可以设置变量和子变量。这样的:
I know you can set variables and "sub-variables". Like:
var msg = "Hello World";
alert(msg);
和也
var msg = {
lipsum: "Lorem Ipsum Dolor Sit Amet"
}
alert(msg.lipsum);
但我在想,如果你能做到的两个,像
var msg = "Hello World" || {
lipsum: "Lorem Ipsum"
}
alert(msg + msg.lipsum);
这样的话,你可以声明一个变量,也有相同的变量是一个对象。显然它不能与我所做的工作要做,但你得到的图片。
That way, you can declare a variable and also have the same variable be an object. Obviously it couldn't be done with what I did, but you get the picture.
任何帮助将是非常美联社preciated!
Any help would be much appreciated!
推荐答案
其实,你可以。
var msg = {
lipsum: "Lorem Ipsum",
toString: function() {return "Hello World!"}
};
这篇关于JavaScript变量和相同的变量也是一个对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文