繁体   English   中英

JavaScript 语法意义

[英]Javascript syntax meaning

给定以下 JavaScript 块:

var n = { a: 1, b: "1.0", c: false, d: true };
var a = "b";

有人可以帮我解释以下表达式:

n.a 
n[ a ]
n.a == n.b 
n.a === n.b 
n.b == n.d 
n.a =n= n.d  
n.c ? "a" : "b" 
n.e   
n.e 
n.e != null 

该代码创建了两个变量,一个对象 ( n ) 和一个字符串 ( a )。 对象的属性可以通过. []运算符。 您应该阅读有关 Javascript 对象如何工作的描述,例如这个

n.a                  // Accesses the 'a' property of n, which currently holds 1
n[ 'a' ]             // Also accesses the 'a' property. Same as above.
n[ a ]               // Since a holds the string 'b', the accesses the
                     // 'b' property of n
n.a == n.b           // Compares n's 'a' property to n's 'b' property
n.a === n.b          // Same, but does a strict comparison so type
                     // and value are compared
n.b == n.d           // Another comparison with different properties
n.a =n= n.d          // Sets n to true. Will cause the lines after this
                     // not to function as expected since n is no longer
                     // an object. See Barmar's comments below.
n.c ? "a" : "b"      // Shorthand for if (n.c) return "a" else return "b" 
n.e                  // Accesses 'e' attribute of n (Currently not set)
n.e != null          // Compares (unset) e attribute to null

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM