if (true) {
var x = 5;
}
console.log(x); // 5
i f (true) {
let y = 5;
}
console.log(y); // ReferenceError: y is not defined1234
/**
* 全局变量上浮
*/
console.log(x === undefined); // logs "true"
var x = 3;
/**
* 方法变量上浮
*/
var myvar = "my value";
// 打印变量myvar结果为:undefined
(function() {
console.log(myvar); // undefined
var myvar = "local value";
})();
上面代码和下面代码是等价的:
/**
* 全局变量上浮
*/
var x;
console.log(x === undefined); // logs "true"
x = 3;
/**
* 方法变量上浮
*/
var myvar = "my value";
(function() {
var myvar;
console.log(myvar); // undefined
myvar = "local value";
})();
version = "1.0.0"; console.log(window.version); //输出1.0.0
parseInt("0xF", 16);
parseInt("F", 16);
parseInt("17", 8);
parseInt(021, 8);
parseInt("015", 10);
parseInt(15.99, 10);
arseInt("15,123", 10);
parseInt("FXX123", 16);
parseInt("1111", 2);
parseInt("15*3", 10);
parseInt("15e2", 10);
parseInt("15px", 10);
parseFloat("3.14"); //返回数字
parseFloat("314e-2"); //返回数字
parseFloat("more non-digit characters"); //返回NaN
0, 117 and -345 //十进制 015, 0001 and -0o77 //八进制 0x1123, 0x00111 and -0xF1A7 //十六进制 0b11, 0b0011 and -0b11 1234 //二进制 浮点数:[(+|-)][digits][.digits][(E|e)[(+|-)]digits]。例如: 3.1415926,-.123456789,-3.1E+12(3100000000000),.1e-23(1e-24)
var car = { manyCars: {a: "Saab", "b": "Jeep"}, 7: "Mazda" };
console.log(car.manyCars.b); // Jeep
console.log(car[7]); // Mazda
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 quote = "He read \"The Cremation of Sam McGee\" by R.W. Service."; console.log(quote); //输出:He read "The Cremation of Sam McGee" by R.W. Service.1。
var str = "this string \ is broken \ across multiple\ lines." console.log(str); // this string is broken across multiplelines.
while (x < 10) {
x++;
}
var x = 1;
{
var x = 2;
}
console.log(x); // outputs 2
var b = new Boolean(false); if (b) // 返回true if (b == true) // 返回false
throw "Error2"; // 字符串类型
throw 42; // 数字类型
throw true; // 布尔类型
throw {toString: function() { return "I'm an object!"; } }; //对象类型
// 创建一个对象类型UserException
function UserException(message) {
this.message = message;
this.name = "UserException";
}
//重写toString方法,在抛出异常时能直接获取有用信息
UserException.prototype.toString = function() {
return this.name + ': "' + this.message + '"';
}
// 创建一个对象实体并抛出它
throw new UserException("Value too high");
function f() {
try {
console.log(0);
throw "bogus";
} catch(e) {
console.log(1);
return true; // 返回语句被暂停,直到finally执行完成
console.log(2); // 不会执行的代码
} finally {
console.log(3);
return false; //覆盖try.catch的返回
console.log(4); //不会执行的代码
}
// "return false" is executed now
console.log(5); // not reachable
}
f(); // 输出 0, 1, 3; 返回 false
function f() {
try {
throw "bogus";
} catch(e) {
console.log('caught inner "bogus"');
throw e; // throw语句被暂停,直到finally执行完成
} finally {
return false; // 覆盖try.catch中的throw语句
}
// 已经执行了"return false"
}
try {
f();
} catch(e) {
//这里不会被执行,因为catch中的throw已经被finally中的return语句覆盖了
console.log('caught outer "bogus"');
}
// 输出
// caught inner "bogus"
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有