简体   繁体   English

我可以将字符串分配给javascript对象变量吗?

[英]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 = {};

  1. I think now my name is a object? 我想我的名字现在是一个对象?

Can I assign a string to this variable name, like name = "Foo"; 我可以给这个变量名分配一个字符串,例如name = "Foo";

  1. Will this work? 这样行吗?
  2. If this work, any cons for this? 如果这项工作可行,对此有何弊端?

(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: 这样做的最大缺点是它会散布错误:

  • You can overwrite your variable unintentionally 您可以无意中覆盖变量
  • Type can be changed though the value will be almost the same like 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
  • You can always check what type you are using with 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.

相关问题 如何将格式字符串中的日期分配给javascript日期变量? - how can I assign a date in format string to a javascript date variable? JavaScript:如何分配与同一对象内的另一个变量相同的变量? - JavaScript: How can I assign a variable the same as another variable inside the same object? Javascript-如何删除字符串的一部分并将其分配给变量以进行操作? 琴弦变化 - Javascript - How can I remove portion of a string and assign it to a variable for manipulation? String is varied 我可以为 JavaScript 中的 1 个定义分配 2 个 object 吗? - Can I assign 2 object for 1 definition in JavaScript? 在 JavaScript 中,如何将对象值数组存储到字符串变量中? - In JavaScript, how can I store an array of object value into string variable? 如果字符串很奇怪,我该如何将字符串粘贴并分配给javascript变量? - how do i paste and assign string to javascript variable, if string is weird? 将空字符串分配给javascript变量 - Assign empty string to javascript variable 为什么我不能使用“ this”将对象中的对象变量分配给JavaScript中该对象中的另一个变量 - Why I cant use “this” to assign a object variable from a object to another variable in that object in JavaScript 如何分配HTML格式的JavaScript变量? - How can I assign a javascript variable in html format? 如何为会话值分配JavaScript变量 - How can I assign a javascript variable to the session value
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM