"30" + 8 ==> "308" "30" - 8 ==> 22 "30" * 8 ==> 240 "30" / 8 = 3.75 "30" << 8 ==> 7680 "30" >> 8 ==> 0
parseInt(string, base) // base = [2,36]
parseInt("15") = 15 // default is decimal
parseInt(0xF) = parseInt("0xF") = parseInt("0xF", 16) = 15
parseInt(017) = parseInt("015") = 15 = parseInt("017", 8) = 15
parseInt(017, 8) = parseInt("015", 8) = 13
parseInt("15*3", 10) ==> parseInt("15", 10) ==> 15
parseInt("321", 2) ==> NaN
See also: parseFloat("string");
var Student = function (name, id) { ... }
var a = new Student("Ric", 101);
var b = new Student("Mary", 301);
a.score = 100; // property 'score' only belongs to 'a'
var a = { i: 10, "jj": 20 }
var a = { i: 10, jj: 20 }
console.log(a."jj"); // ERROR!!
console.log(a.jj); // OK!
ric.name; // “Ric”
ric."name"; // ERROR!!
ric[name]; // undefined, if var name is undefined
ric["name"]; // “Ric”
ric[0]; // undefined, if ric is not an array
var a;
if (someExpression) { a = "A"; }
else { a = "B"; }
// assume someExpression is false
ric[a] = 70;
console.log(ric.A); // undefined
console.log(ric.B); // 70
var car = {
manyCars: { a: "Saab", "b": "Jeep"},
7: "Mazda",
"": "An empty string",
"!": "Bang!"
};
car.manyCars.a; // "Saab" car.manyCars."a"; // ERROR!!
car.manyCars.b; // "Jeep" car.manyCars."b"; // ERROR!!
car.manyCars[a]; // undefined car.manyCars["a"]; // "Saab"
car.manyCars[b]; // undefined car.manyCars["b"]; // "Jeep"
car."7"; // ERROR!! car.7; // ERROR!!
car["7"]; // "Mazda"; car[7]; // "Mazda"
car.""; // ERROR!! car[""]; // "An empty string"
car."!"; // ERROR!! car.!; // ERROR!!
car["!"]; // "Bang!"
var obj = { property_1: value_1, ... }
function Car(make, model, year) { this.make = ... }
// is the same as
var Car = function(make, model, year) { this.make = ... }
// Then you can use "new" to create objects
var car1 = new Car("Toyota", "Wish", 2015);
// Using an existing object as prototype
var obj = { value: 0 }
var newObj = Obejct.create(obj);
console.log(newObj.value); // 0
newObj.value = 10; // assign a new value
// #1 Function call
function Car(make) { this.make = make; }
var car = Car("BMW");
console.log(car); // undefined
console.log(car.make); // ERROR
// #2 Constructor call
function Car(make) { this.make = make; }
var car = new Car("BMW");
console.log(car); // { make: "BMW" }
console.log(car.make); // "BMW"
// #3 Function call to a ref to an anonymous function
var Car = function(make) { this.make = make; }
var car = Car("BMW");
console.log(car); // undefined
console.log(car.make); // ERROR
// #4 Constructor call to a ref to an anonymous function
var Car = function(make) { this.make = make; }
var car = new Car("BMW");
console.log(car); // { make: "BMW" }
console.log(car.make); // "BMW"
// #5 Ref to a ref to an anonymous function
var Car = function(make) { this.make = make; }
var car = Car;
console.log(car); // is a function
console.log(car.make); // undefined
// #6 Constructor call to a ref to an anonymous function
var Car = function(make) { this.make = make; }
var car = new Car; // missing parameter
console.log(car); // is a object
console.log(car.make); // undefined
// #7 Create an object from another object as a prototype
var Car = function(make) { this.make = make; }
var car = new Car("BMW");
console.log(car); // { make: "BMW" }
console.log(car.make); // "BMW"
var myCar = Object.create(car);
console.log(myCar); // { } <-- no own property
console.log(myCar.make); // "BMW" <-- inherited property
var Car = function(make) { this.make = make; }
var car = new Car("BMW");
var myCar = Object.create(car);
myCar.year = 2018;
console.log(myCar);
// You should see
{
year: 2018,
__proto__: Car { // Note: not "car"
make: "BMW",
__proto__: {
constructor: f (make),
__proto__: Object
}
}
}
var Car = function(make) { this.make = make; }
var car = new Car("BMW");
var myCar = Object.create(car);
myCar.year = 2018;
var Car = function(make, model) {
this.make = make; this.model = model; }
var car = new Car("BMW", "X5");
var myCar = Object.create(car);
myCar.model = "X3"; // over-write
myCar.year = 2018; // own property
console.log(myCar.driver); // undefined
Object.keys(myCar); // ["year"]
for (var i in myCar)
console.log(i); // [ "year", "make", "model"]
var Obj = function() { this.a = 10; this.b = 20; }
var obj1 = new Obj();
Object.keys(obj1); // [ "a", "b" ]
var obj2 = Object.create(obj1);
obj2.c = 30;
console.log(obj2); // { c: 30, __proto__: Obj }
Object.keys(obj2); // [ "c" ]
console.log(obj2.a); // 10
obj2.a = 40; // overwrite and make it own property
console.log(obj2); // { c: 30, a: 40, __proto__: Obj }
Object.keys(obj2); // [ "c", "a" ]
var Car = function(make) { this.make = make; }
var car = new Car("BMW");
var myCar = Object.create(car);
myCar.year = 2018;
console.log(Car.prototype); // You should see
{
constructor: f (make),
__proto__: Object
}
console.log(car.prototype); // undefined
console.log(myCar.prototype); // undefined
var Car = function(make) { this.make = make; }
var car = new Car("BMW");
var myCar = Object.create(car);
myCar.year = 2018;
Car.prototype.model = "X5";
console.log(Car.prototype); // You should see
{
model: "X5";
constructor: f (make),
__proto__: Object
}
console.log(myCar.model); // "X5"
var Car = function(make) {
this.make = make;
this.drive = function () {
console.log(this.make + " is driving");}
}
var car = new Car("BMW");
car.drive(); // "BMW is driving"
var myCar = Object.create(car);
myCar.make = "Lexus";
myCar.drive(); // "Lexus is driving"
Car.prototype.park = function () { // add a function
console.log(this.make + " is parking"); }
myCar.park(); // "Lexus is parking"
如前所述,當初 JavaScript 在設計上,有很多會讓 programmer 混淆的語法問題,例如:"this" bind 錯誤,在 ES6 提出 arrow function 後獲得很大的改善
Arrow function 就像是 lamda function in Python
(argument1, argument2, ... argumentN) => {
// function body
}
const add = (a, b) => a + b;
const getFirst = array => array[0];
var materials = [ 'Hydrogen', 'Helium', 'Lithium', 'Beryllium'];
console.log(materials.map(material => material.length));
// expected output: Array [8, 6, 7, 9]
var todoListData = [ ... ];
todoListData.filter(elem => !elem.isComplete);
if (todoListData.some(ele => ele.isComplete))
doSomething...