js中的小知识点

  • 将字符串转换为数字:
+'123'
result:123

+'123' + + '234';
result: 357
  • 关于数组的长度
var myList = ['home', , 'school'];
myList.length: 3

var myList = ['home', , 'school', ];
myList.length: 3

var myList = ['home', , 'school', , ];
myList.length: 4

//If you include a trailing comma at the end of the list of elements, the comma is ignored.Only the last comma is ignored.
  • 关于对象
var unusualPropertyNames = {
  "": "An empty string",
  "!": "Bang!"
}
console.log(unusualPropertyNames."");   // SyntaxError: Unexpected string
console.log(unusualPropertyNames[""]);  // An empty string
console.log(unusualPropertyNames.!);    // SyntaxError: Unexpected token !
console.log(unusualPropertyNames["!"]); // Bang!

var foo = {a: "alpha", 2: "two"};
console.log(foo.a);    // alpha
console.log(foo[2]);   // two
//console.log(foo.2);  // Error: missing ) after argument list
//console.log(foo[a]); // Error: a is not defined
console.log(foo["a"]); // alpha
console.log(foo["2"]); // two
  • String
"one line \n another line"
result: one line
another line


var str = "this string \
is broken \
across multiple\
lines."
console.log(str);   // this string is broken across multiplelines.
//Chrome浏览器的控制台上想要不得结果的回车方法:shift + enter

var poem = 
"Roses are red,\n\
Violets are blue.\n\
I'm schizophrenic,\n\
And so am I."
  • 关于判断
var b = new Boolean(false);
if(b) {
    alert(b);
}
result: 弹出框false
why?
看一下b的内容:Boolean {[[PrimitiveValue]]: false}
所以b是有内容的而不是false
//Do not confuse the primitive boolean values true and false with the true and false values of the Boolean object. For example:
只有如下内容会被解释成false
false
undefined
null
0
NaN
the empty string ("")
  • 关于switch
switch (expression) {
  case label_1:
    statements_1
    [break;]
  case label_2:
    statements_2
    [break;]
    ...
  default:
    statements_def
    [break;]
}
//By convention, the default clause is the last clause, but it does not need to be so.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值