f2e
var number = 10;
var str = "Hello";
var bool = true;
var array = [];
var num = 100;
parseInt("500", 10);
isNaN(1); // false
isNaN('a'); // true
var text = 'Hello World';
text.length; // 11
text.replace('World', 'JS'); // Hello JS
text.toUpperCase(); // HELLO WORLD
Boolean(false) // false
Boolean(0) // false
Boolean('') // false
Boolean(null) // false
Boolean(undefined) // false
Boolean(NaN) // false
只要一個變數在初始的時候未給予任何值的時候,就會產生 undefined
var a;
typeof a; // undefined
number、string與boolean,會在必要的時候
自動型態轉換為對應的包裹物件
'6' + 2 // '62'
2 + '6' // '26'
'6' - '2' // 4
'6' - 2 // 4
'6' * '2' // 12
'6' / '2' // 3
1 + true // 2
1 + false // 1
typeof 1; // 'number'
typeof 'Hello'; // 'string'
typeof false; // 'boolean'
typeof []; // 'object'
typeof {}; // 'object'
typeof null; // 'object'
typeof undefined; // 'undefined'
var array = [
10,
"Hello",
false,
{a:1},
function(){alert("Hello")}
];
function print(s) {
console.log(s);
}
or
var print = function(s) {
console.log(s);
}
print('Hello');
{ key: value }
// bad
var person = new Object();
person.name = 'Andy';
person.age = 27;
person.speak = function() {
alert('Hello');
}
// good
var person = {
name: 'Andy',
age: 27,
speak: function() {
alert('Hello');
}
}
function Person(name, age){
this.name = name;
this.age = age;
}
var person = new Person("Andy", 27);
console.log(person.name); // 'Andy'
console.log(person.age); // 27
var _class = {
name: '一年一班',
students: [{
name: '學生1',
seatNo: 1
}, {
name: '學生2',
seatNo: 2
}]
}
for(var i=0; i<_class.students.length; i++) {
var student = _class.students[i];
console.log(student.name);
console.log(student.seatNo);
}
var a = 1;
var b = a;
b = 2;
// a = 1
// b = 2
1 == 1 // true
var a = [1,2,3];
var b = a;
b.push(4);
// a = [1,2,3,4]
// b = [1,2,3,4]
var a = {t:1};
var b = a;
b.t = 2;
// a = {t:2}
// b = {t:2}
[1] == [1] // false
{a:1} == {a:1} // false
function Person(name, age){
this.name = name || 'person';
this.age = age || 18;
}
var p1 = new Person('Andy', 27);
// 'Andy' 27
var p2 = new Person();
// 'person' 18
10 == '10' // true
10 === '10' // false
10 != '10' // false
10 !== '10' // true
function person(obj) {
console.log(obj.name);
console.log(obj.age);
}
var data = {name:'andy', age: 27};
person(data);
// 'andy'
// 27
var arr = ["jack", "john", "may", "su", "Ada"];
var str = arr.join("、");
// "jack、john、may、su、Ada"
var alpha = ["a", "b", "c"];
var numeric = [1, 2, 3];
var alphaNumeric = alpha.concat(numeric);
// ["a", "b", "c", 1, 2, 3]
var numbers = [1,2,3,4,5,6,7,8,9,10];
var newNumbers = numbers.map(function(element, index) {
return element * 2;
});
// [2, 4, 6, 8, 10, 12, 14, 16, 18, 20]
// bad
var count = 0;
numbers.map(function(element) {
count++;
return element * 2;
});
var numbers = [1,2,3,4,5,6,7,8,9,10];
var newNumbers = numbers.filter(function(number){
return number % 2 == 0;
});
// [2, 4, 6, 8, 10]
<body onload="myFunction()">
</body>
<script>
function myFunction() {
document.getElementById('text').value = 'text';
}
</script>
// jQuery
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
<script>
$().ready(function() {
$('#text').val('tet');
})
</script>
<button id="btn1" class="btn"/>
document.getElementById('btn1');
document.getElementsByClassName('btn');
document.getElementsByTagName('button');
// jQuery
$('#btn1') // id selector
$('.btn') // class selector
$('button') // element selector
<input id="search" type="text" value="123" />
document.getElementById('search').value; // 123
// jQuery
$('#search').val(); // 123
<button id="btn1" class="btn"/>
document.getElementById('btn1').onclick = function(){
alert('Hello');
}
// jQuery
$('#btn1').click(function() {
alert('Hello');
})
<ul id="list"></ul>
$('#list').append('<li>item1</li>')
<ul id="list">
<li>item1</li>
</ul>
function Person(name, age) {
this.name = name;
this.age = age;
this.toString = function() {
return '[' + this.name + ', ' + this.age + ']';
};
}
function Person(name, age) {
this.name = name;
this.age = age;
}
Person.prototype.toString = function() {
return '[' + this.name + ', ' + this.age + ']';
};
var x = 10;
function func() {
console.log(x);
}
func();
// 10
var x = 10;
function func() {
console.log(x);
var x = 20;
console.log(x)
}
func();
// undefined
// 20
function sum() {
var x = 10;
return function (y) {
console.log(x+y);
}
}
var func = sum();
func(200); // 210
將函式當作另一個函式的參數,由另外的函式來呼叫
function 接電話(name, phone, doSomething) {
console.log('來電者:' + name + ', 號碼:' + phone);
doSomething();
}
function 打招呼() {
alert('Hello');
}
接電話('Andy', '0987654321', 打招呼);
// 轉碼前
input.map(item => item + 1);
// 轉碼後
input.map(function (item) {
return item + 1;
});
ES5:
function Person(name, age){
this.name = name || 'person';
this.age = age || 18;
}
ES6:
function Person(name = 'person', age = 18){
this.name = name;
this.age = age;
}
ES5:
var name = firstName + '_' + lastName;
ES6:
let name = `${firstName}_${lastName}`;
ES5:
var SQL = 'SELECT * ' +
'FROM TABLE ' +
'WHERE NAME="' + name + '"'
ES6:
const SQL =
`
SELECT *
FROM TABLE
WHERE NAME='${name}'
`
ES5:
var Person = function(name) {
console.log(name);
}
ES6:
const Person = (name) => {
console.log(name);
}
ES5:
function Person(name, age){
this.name = name;
this.age = age;
}
Person.prototype.say = function() {
alert('Name:' + this.name +', Age:' + this.age);
}
var person = new Person("Andy", 27);
person.say();
ES6:
class Person {
constructor(name, age) {
this.name = name;
this.age = age;
}
say() {
alert(`Name:${this.name}, Age:${this.age}` );
}
}
let person = new Person('Andy', 27);
person.say();