[英]Can I assign a string to a javascript object variable?
This seems an easy question for experienced developer, but I did not find the answer in stack overflow. 对于有经验的开发人员来说,这似乎是一个简单的问题,但是我没有在堆栈溢出中找到答案。
If I declare a variable like this, var name = {};
如果我这样声明一个变量,则
var name = {};
Can I assign a string to this variable name, like name = "Foo";
我可以给这个变量名分配一个字符串,例如
name = "Foo";
(I know I can declare name as var name = "";
But I remember in javascript, anything is object (including string, so string is also an object)). (我知道我可以将name声明为
var name = "";
但是我记得在javascript中,任何东西都是对象(包括字符串,所以string也是一个对象))。
JavaScript is dynamically typed language. JavaScript是动态类型的语言。 In a nutshell, this means JS engine automatically decides what type to use depending on the type of value you are assigning to it.
简而言之,这意味着JS引擎会根据您为其分配的值的类型自动决定要使用的类型。 And it also means you can change the type of your variable anytime you want.
这也意味着您可以随时更改变量的类型。
var name = {} //name is object
name = "" // name is now a string
name = function (a) { return a + a } //it's now a function
The biggest con for this is that it spreads bugs: 这样做的最大缺点是它会散布错误:
1 + "" // still 1 but is not a number anymore. It's a string now
1 + "" // still 1 but is not a number anymore. It's a string now
1 + "" // still 1 but is not a number anymore. It's a string now
typeof()
function, but it won't help you in some cases typeof( new Array() ) // is object
typeof()
函数检查正在使用的类型,但是在某些情况下,它不会帮助您typeof( new Array() ) // is object
There are plenty of nuances you'd need to learn about dynamic typing. 您需要了解许多有关动态类型的细微差别。 So much that Microsoft decided to develop TypeScript - a typed superset of JavaScript invented to make the life of JS developers easier.
如此之多,微软决定开发TypeScript-一种JavaScript的类型化超集,其发明是为了简化JS开发人员的生活。 It can do anything JS can plus several features from strictly typed languages
它可以做JS可以做的所有事情,还可以加上严格类型化语言的一些功能
As yBrodsky mentioned, you can do that in javascript as JS variables are not typed 如yBrodsky所述,您可以在javascript中完成此操作,因为未键入JS变量
you can use typeof
operator to examine the type of the variable 您可以使用
typeof
运算符检查变量的类型
msg = "hello";<br>
typeof msg ;
// returns "string"
msg = { a:"b"};<br>
typeof msg;
// returns "object"
Javascript is a dynamically typed language.Variable itself does not hold any datatype its the value inside the variable which defines its type. Javascript是一种动态类型化的语言,变量本身不包含任何数据类型,其值位于定义其类型的变量中。
var name = {};
var name = {}; // will lead to name as "object" type
//将导致名称为“对象”类型
name = "";名称=“”; // will lead to name as "string" type
//将导致名称为“字符串”类型
name = [] ;名称= []; // will lead to name as "object" type
//将导致名称为“对象”类型
In Javascript, everything is not an object. 在Javascript中,一切都不是对象。 There are two things primitives and natives.
有原始和本地两种东西。
These types are called primitives: null, undefined, number, string, boolean, symbol. 这些类型称为原语:null,undefined,number,string,boolean,symbol。 When we do typeof primitives it will not return an object(except typeof null returns object).
当我们执行typeof原语时,它将不会返回对象(typeof null返回对象除外)。
Natives: String(), Number(), Boolean(), Array(), Object(), Function(), RegExp(), Date(), Error(), Symbol(). 本机:String(),Number(),Boolean(),Array(),Object(),Function(),RegExp(),Date(),Error(),Symbol()。 When we do typeof natives then it will return an object.
当我们进行本地类型的输入时,它将返回一个对象。
Native are created with the new keyword: 本机是使用new关键字创建的:
var hey = new String("rajat");
var hey = new String(“ rajat”);
And new will always return an object. 并且new总是会返回一个对象。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.