javascript - 具有不同类型的 Javascript 中的变量赋值的可视化/实用/真实世界示例

标签 javascript

想要了解变量在 javascript 中如何在分配不同类型的值时工作。

  • 类型 1:字符串 bool 值
  • 类型 2:数组对象函数正则表达式日期

目前引用了两本引起混淆的书。

let mood = "light";
console.log(mood);
// → light
mood = "dark";
console.log(mood);
// → dark

You should imagine bindings as tentacles, rather than boxes. They do not contain values; they grasp them—two bindings can refer to the same value. A program can access only the values that it still has a reference to. When you need to remember something, you grow a tentacle to hold on to it or you reattach one of your existing tentacles to it.

We saw that object values can be modified. The types of values discussed in earlier chapters, such as numbers, strings, and Booleans, are all immutable—it is impossible to change values of those types. You can combine them and derive new values from them, but when you take a specific string value, that value will always remain the same. The text inside it cannot be changed. If you have a string that contains "cat", it is not possible for other code to change a character in your string to make it spell "rat".

来自 Eloquent Javascript


JavaScript lets you give names to values using variables. You can think of a variable as a box that you can fit one thing in. If you put something else in it, the first thing goes away

进一步

Cool! The value of the variable isn’t set in stone, though (they’re called variables because they can vary), and if you want to update it, just use = again:

发件人:Javascript for Kids

不同的作者建议不同的想象力来理解概念。除了盒子,触 Angular 还有什么例子可以帮助我了解上下文。 需要了解存储在变量中的内容(值/地址/值的二进制表示)。有没有阐明核心概念的视频、图片资源。

面试官对如何回答这样的问题(字符串类型的变量换其他值)有这样的看法,也可以有自己不同的理解方式

最佳答案

在阅读了互联网上的许多资料后,大多数人将变量视为容器,而不是数据中的变量本身。 参见 Storing the information you need — Variables .他们说

We say variables contain values. This is an important distinction to make. Variables aren't the values themselves; they are containers for values. You can think of them being like little cardboard boxes that you can store things in.

还有 W3Schools JavaScript Variables , 他们还说

JavaScript variables are containers for storing data values.

因此,在我的结论中,在具有不同类型的 Javascript 中,最直观/实用/真实世界的变量赋值示例将其想象成一个盒子。

关于javascript - 具有不同类型的 Javascript 中的变量赋值的可视化/实用/真实世界示例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55392489/

相关文章:

javascript - 单击链接时更改变量

php - 在 firebug 或 chrome 开发人员中更改客户端的 javascript 变量

javascript - 如何在 angularjs 中使用 OOP 类

javascript - 2012 年 10 月 4 日至 10 月 5 日 javascript 额外增加一小时

javascript - 重新加载 KeyDown 和 Click 事件后仅在第二次单击后有效

javascript - 如何用DOM检索style.top,还有其他方法吗?

javascript - 是否可以将流类型包装在不可变容器中?

asp.net - JavaScript 中的十进制比较

javascript - 对象方法不是函数

javascript - 触发子列表在类更改时的可见性