赞
踩
const arr = [1, 2, 3];
console.log(Array.isArray(arr)); // true
console.log(Object.prototype.toString.call(arr) === "[object Array]"); // true
const obj = {a: 1, b: 2, c: 3};
for (const prop in obj) {
console.log(prop + ": " + obj[prop]);
}
// 输出:a: 1 b: 2 c: 3
console.log(1 + "2" + "2"); // "122"
console.log(1 + +"2" + "2"); // "32"
console.log(1 + -"1" + "2"); // "02"
console.log(+"1" + "1" + "2"); // "112"
console.log("A" - "B" + "2"); // "NaN2"
console.log("A" - "B" + 2); // NaN
const str = "123";
const num1 = parseInt(str); // 123
const num2 = parseFloat(str); // 123.0
const num = 123;
const str = num.toString(); // "123"
console.log(false == '0'); // true
console.log(false === '0'); // false
const arr = [1, 2, 3];
arr.splice(1, 1);
console.log(arr); // [1, 3]
function Animal(name) { this.name = name; } Animal.prototype.sayName = function() { console.log(this.name); }; function Dog(name) { this.breed = "Unknown"; Animal.call(this, name); } Dog.prototype = Object.create(Animal.prototype); Dog.prototype.constructor = Dog; Dog.prototype.sayBreed = function() { console.log(this.breed); }; const myDog = new Dog("Fido"); myDog.sayName(); // "Fido" myDog.breed = "Husky"; myDog.sayBreed(); // "Husky" // ES6的class关键字和extends关键字 class Animal { constructor(name) { this.name = name; } sayName() { console.log(this.name); } } class Dog extends Animal { constructor(name) { super(name); this.breed = "Unknown"; } sayBreed() { console.log(this.breed); } } const myDog = new Dog("Fido"); myDog.sayName(); // "Fido" myDog.breed = "Husky"; myDog.sayBreed(); // "Husky"
function outer() {
var count = 0;
function inner() {
count +=1;
console.log(count);
}
return inner;
}
var counter = outer();
counter(); // 输出 1
counter(); // 输出 2
counter(); // 输出 3
在这个例子中,函数 outer
返回了一个内部函数 inner
。每次调用内部函数 inner
,它的闭包都会保留变量 count
的值,所以 count
的值会一直增加。
console.log(x); // undefined
var x = 10;
foo(); // "bar"
function foo() {console.log("bar");}
try {
// 可能会抛出错误的代码
} catch (error) {
// 处理错误的代码
} finally {
// 无论是否发生错误都会执行的代码
}
const arr = [1, 2, 3, 4, 5];
console.log(arr.indexOf(3)); // 2
console.log(arr.includes(3)); // true
更多详见:
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。