请向我解释这个代码! (JavaScript)的 [英] Please explain this code to me! (Javascript)

查看:81
本文介绍了请向我解释这个代码! (JavaScript)的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个名为chatRoom的变量,定义如下:

I've a variable by the name "chatRoom", defined as follows:

var chatRoom = {
	'all' : []
};



这里,我假设聊天室是一个JSON对象/数组。



现在,这里是另一段代码


Here, I assume chatroom is a JSON object/array.

Now, here is another piece of code

if (chatRoom[to]) {
		chatRoom[to].push(newChatEl);
	} else
		chatRoom[to] = [ newChatEl ];



这里,to包含一些字符串值。





我是编程新手,对Java有一点了解,但对JavaScript几乎一无所知。我在Java中学到的是,我们必须在if关键字后面的括号中放置一个布尔值。但在JavaScript中,chatroom [to]看起来不像布尔值true / false。



1.您如何在这里解释chatRoom [Harry]这个词?字符串Harry是否存储在JSON对象chatRoom中作为chatRoom = {'all':[Harry]}?如果我们要引用Harry,我们该怎么做:chatRoom.all [0]或chatRoom [0]?



2.你是什么意思chatRoom [哈里] .push(newChatEl)?这里chatRoom不是一个数组,而是一个JSON对象。我们在chatRoom中添加一个包含newChatEl的新节点吗?作为参考,newChatEl是一堆Html代码(创建了一些 div span 元素,并将它们的引用传递给newChatEl)。因此,newChatEl甚至不是String对象。如果newChatEl不是String,怎么可以推送到chatRoom [Harry]?





3.在 else 部分代码,我们创建一个数组并将newChatEl作为其第一个元素传递给它吗?



对不起,如果问题很愚蠢,但我需要完全理解它。

谢谢!



我的尝试:



需要一些解释,已在问题中解释过。


Here, "to" holds some string value.


I'm new to programming and have a little knowledge of Java but near to none knowledge of JavaScript. What I've learned in Java is that, we have to a put a statement which results in a Boolean value in the parenthesis after "if" keyword. But here in JavaScript "chatroom[to]" does not look like a Boolean true/false.

1. How would you interpret term "chatRoom[Harry]" here? Is the string "Harry" stored in the JSON object chatRoom as chatRoom={'all':[Harry]}? If we have to refer to Harry, how do we do it: chatRoom.all[0] or chatRoom[0]?

2. What do you mean by chatRoom[Harry].push(newChatEl)? Here chatRoom is not an array, but a JSON object. Are we adding a new node in Harry in chatRoom which contains newChatEl? For reference, newChatEl is a bunch of Html codes (Some div and span elements are created and their reference is passed to newChatEl). So, newChatEl is not even a String object. And if "newChatEl"is not String, how can it be pushed to chatRoom[Harry]?


3. In the else part of the code, are we creating an array and passing newChatEl in it as its first element?

Sorry, if the questions are silly, but I need to understand it completely.
Thank you!

What I have tried:

Need some explanation, already explained in the question.

推荐答案

Quote:

var chatRoom = {
    'all' : []
};



首先,尽管有些人可能会告诉你, JSON [ ^ ]仅引用包含序列化对象或数组的字符串。你有什么对象文字。



语法和类型 - JavaScript | MDN [ ^ ]



这将声明一个名为 chatRoom 的新变量,其中包含一个具有单个属性的对象叫全部。该属性包含一个空数组。





现在,在Javascript中,有两种方法可以访问对象的属性。您可以使用标准点符号:


Firstly, despite what some people might tell you, JSON[^] only ever refers to a string containing a serialized object or array. What you have there is an object literal.

Grammar and types - JavaScript | MDN[^]

This declares a new variable called chatRoom, which contains an object with a single property called all. That property contains an empty array.


Now, in Javascript, there are two ways you can access a property of an object. You can use the standard dot-notation:

var foo = chatRoom.all;



或者您可以使用括号符号:


Or you can use bracket notation:

var bar = chatRoom['all'];



第二个选项如果你没有特别有用在编写代码时不知道属性的名称:


The second option is particularly useful if you don't know the name of the property when you write the code:

var to = 'all';
var baz = chatRoom[to];



使用对象 - JavaScript | MDN [ ^ ]





Javascript更古怪的一个领域是truthy和falsey值的概念。基本上,几乎所有东西都可以被视为 bool ,并且有一些标准规则来确定它是真还是假。

Truthy and Falsy:当JavaScript中的所有内容不相同时 - SitePoint [ ^ ]



值不存在的属性 undefined 。根据规则,该值为falsey,因此:


Working with objects - JavaScript | MDN[^]


One of the more quirky areas of Javascript is the concept of "truthy" and "falsey" values. Basically, almost everything can be treated as if it were a bool, with some standard rules to determine whether it's "true" or "false".
Truthy and Falsy: When All is Not Equal in JavaScript — SitePoint[^]

The value of a property that doesn't exist is undefined. According to the rules, that value is "falsey", so:

if (chatRoom[to]) {

将执行 if 如果属性存在则阻止,如果不存在则阻止 else 阻止。





现在,您可以看到您的代码阻止:

will execute the if block if the property exists, and the else block if it doesn't.


So now, you can see that your code block:

if (chatRoom[to]) {
    chatRoom[to].push(newChatEl);
} else {
    chatRoom[to] = [ newChatEl ];
}

确定 chatRoom 是否包含具有指定名称的属性。如果是,则将 newChatEl 添加到该属性。如果没有,它会创建属性并使用包含 newChatEl 的新数组对其进行初始化。





1)除非变量包含字符串'all',否则此代码不会触摸所有数组。



2) chatRoom [x] .push(y )将值 y 添加到存储在 x 中命名的属性中的数组中。属性值是数组,而不是 chatRoom



3)是, [ x] 数组文字的语法 [ ^ ]包含单个值。

determines whether the chatRoom contains a property with the specified name. If it does, it adds the newChatEl to that property. If it doesn't, it creates the property and initializes it with a new array containing newChatEl.


1) Unless the variable to contains the string 'all', this code will not touch the all array.

2) chatRoom[x].push(y) adds the value y to the array stored in the property named in x. The property value is the array, not chatRoom.

3) Yes, [x] is the syntax for an array literal[^] containing a single value.


这篇关于请向我解释这个代码! (JavaScript)的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆