function assert(condition, message) {
if (!condition) {
throw new Error(message || "断言失败");
}
}
var a1 = true && true; var a2 = true && false; var a3 = false && true; var a4 = false && 3 == 4; var a5 = "Cat" && "Dog"; var a6 = false && "Cat"; var a7 = "Cat" && false;
console.log(a1, a2, a3, a4, a5, a6, a7);
var o1 = true || true; var o2 = false || true; var o3 = true || false; var o4 = false || 3 == 4; var o5 = "Cat" || "Dog"; var o6 = false || "Cat"; var o7 = "Cat" || false;
console.log(o1, o2, o3, o4, o5, o6, o7);
var n1 = !true; var n2 = !false; var n3 = !"Cat";
console.log(n1, n2, n3);
var myString = "alpha";
myString += "bet";
var x = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9];
var a = [x, x, x, x, x];
for (var i = 0, j = 9; i <= j; i++, j--)
console.log("a[" + i + "][" + j + "]= " + a[i][j]);
xx = 42;
var yy = 43;
myobj = new Number();
myobj.h = 4; var res1 = delete xx; var res2 = delete yy; var res3 = delete Math.PI; var res4 = delete myobj.h; var res5 = delete myobj;
console.log(res1, res2, res3, res4, res5);
assert(res1, "delete xx failed");
assert(!res2, "delete yy failed");
assert(!res3, "delete Math.PI failed");
assert(res4, "delete myobj.h failed");
assert(res5, "delete myobj failed");
console.log("xx =", typeof xx !== "undefined" ? xx : "undefined");
console.log("yy =", typeof yy !== "undefined" ? yy : "undefined");
console.log("Math.PI =", Math.PI);
console.log("myobj =", typeof myobj !== "undefined" ? myobj : "undefined");
var trees = new Array("redwood", "bay", "cedar", "oak", "maple");
delete trees[3];
if (3 in trees) {
console.log("删除 trees[3] 失败");
} else {
console.log("删除 trees[3] 成功");
}
assert(!(3 in trees), "删除 trees[3] 失败");
console.log("trees length =", trees.length);
var trees = new Array("redwood", "bay", "cedar", "oak", "maple");
trees[3] = undefined;
assert(3 in trees, "trees[3] 设置为 undefined 失败");
if (3 in trees) {
console.log("trees[3] 设置为 undefined 成功");
} else {
console.log("trees[3] 设置为 undefined 失败");
}
console.log("trees length =", trees.length);
var myFun = new Function("5 + 2");
typeof myFun; assert(typeof myFun === "function", "typeof myFun should be 'function'");
console.log("myFun() =", myFun());
console.log("Implicit return:", new Function("5 + 2")());
console.log("Explicit return:", new Function("return 5 + 2")());
console.log("Eval:", eval("5 + 2"));
var shape = "round";
typeof shape; assert(typeof shape === "string", "typeof shape should be 'string'");
var size = 1;
typeof size; assert(typeof size === "number", "typeof size should be 'number'");
var today = new Date();
typeof today; assert(typeof today === "object", "typeof today should be 'object'");
typeof dontExist; assert(typeof dontExist === "undefined", "typeof dontExist should be 'undefined'");
typeof true; typeof null; assert(typeof true === "boolean", "typeof true should be 'boolean'");
assert(typeof null === "object", "typeof null should be 'object'");
typeof 62; typeof "Hello world"; assert(typeof 62 === "number", "typeof 62 should be 'number'");
assert(typeof "Hello world" === "string", "typeof 'Hello world' should be 'string'");
typeof Math.LN2; assert(typeof Math.LN2 === "number", "typeof Math.LN2 should be 'number'");
typeof eval; typeof parseInt; typeof shape.split; console.log("typeof eval =", typeof eval);
console.log("typeof parseInt =", typeof parseInt);
console.log("typeof shape.split =", typeof shape.split);
assert(typeof eval === "function", "typeof eval should be 'function'");
assert(typeof parseInt === "function", "typeof parseInt should be 'function'");
assert(typeof shape.split === "function", "typeof shape.split should be 'function'");
typeof Date; typeof Function; typeof Math; console.log("typeof Math =", typeof Math);
typeof String; console.log("typeof String =", typeof String);
assert(typeof Date === "function", "typeof Date should be 'function'");
assert(typeof Function === "function", "typeof Function should be 'function'");
assert(typeof Math === "object", "typeof Math should be 'object'");
assert(typeof String === "function", "typeof String should be 'function'");
void 9+5;
void (9+5);
void "expression" * 90;
console.log("void operator tests passed." * 90);
void ("expression");
var trees = new Array("redwood", "bay", "cedar", "oak", "maple");
assert(0 in trees, "0 in trees should be true");
assert(3 in trees, "3 in trees should be true");
assert(!(6 in trees), "6 in trees should be false");
assert(!("bay" in trees), "'bay' in trees should be false (you must specify the index number, not the value at that index)");
assert("length" in trees, "'length' in trees should be true (length is an Array property)");
assert("PI" in Math, "'PI' in Math should be true");
var myString = new String("coral");
assert("length" in myString, "'length' in myString should be true");
var mycar = { make: "Honda", model: "Accord", year: 1998 };
assert("make" in mycar, "'make' in mycar should be true");
assert("model" in mycar, "'model' in mycar should be true");
assert("year" in mycar, "'year' in mycar should be true");
var theDay = new Date(1995, 12, 17);
assert(theDay instanceof Date, "theDay should be instance of Date");
var a = 1;
var b = 2;
var c = 3;
a + b * c; assert(a + b * c === 7, "a + b * c should be 7");
(a + b) * c; assert((a + b) * c === 9, "(a + b) * c should be 9");
a * c + b * c; assert(a * c + b * c === 9, "a * c + b * c should be 9");
var v1 = [ 1, 2, 3 ].map(i => i*i);
console.log(v1);
assert(Array.isArray(v1), "v1 should be an array");
assert(JSON.stringify(v1) === JSON.stringify([1, 4, 9]), "v1 should be [1, 4, 9]");
var abc = [ "A", "B", "C" ];
abc = abc.map(letters => letters.toLowerCase());
console.log(abc);
assert(Array.isArray(abc), "abc should be an array");
assert(JSON.stringify(abc) === JSON.stringify([ "a", "b", "c" ]), "abc should be [ 'a', 'b', 'c' ]");
var object = {};
console.log(object.property); object.property;
object["property"];
var maybeObject = null;
maybeObject?.property;
maybeObject?.[property];
var maybeFunction;
maybeFunction?.();
class ObjectType {
constructor(param1, param2, paramN) {
this.param1 = param1;
this.param2 = param2;
this.paramN = paramN;
}
}
const objectName = new ObjectType("param1", "param2", "paramN");
console.log(objectName.param1);
console.log(objectName.param2);
console.log("All tests passed.");