先來把基本的跑過一下,否則看起 node.js example code 真的很混亂!
This basic from 'NODESCHOOL'
HOW TO GET !?
Learn the basics of JavaScript. No previous programming experience required. npm install -g javascripting
NOTES FOR JAVASCRIPT
//Array_basic
console.log('---- Array_basic ----');
var arr = [1,2,3,4,5];
for (var i = 0; i < arr.length; i++) {
console.log("arr[%d] = %d", i, arr[i]);
}
//foreach
console.log('---- FOREACH ----');
for(i in arr){
console.log("array[%d] = %d", i, arr[i]);
}
//Dictonary
console.log('---- Dictonary ----');
var dict = {name:"charles", age:30};
dict["email"] = "charles@gmail.com";
dict.tel = "0917-211946";
for (var key in dict) {
console.log("key=" + key + " value=" + dict[key]);
}
console.log("age :" + dict.age);
console.log("birthday :" + dict.birthday);
//ARRAY FILTERING
console.log('---- ARRAY FILTERING ----');
var pets = ['cat', 'dog', 'elephant'];
var filtered = pets.filter(function (pet) {
return (pet !== 'elephant');
});
for (var i = 0; i < filtered.length; i++) {
console.log("filtered[%d] = %s", i, filtered[i]);
}
//string Length
console.log('---- String Length ----');
var str_ex = 'example string';
console.log('String Length ======\nThis ' + str_ex + ' lens is ' +str_ex.length);
//Revising Strings 校訂,修正,再校稿(vt.)校訂,修正,校正
console.log('---- Revising Strings ----');
var str_ex = 'This example exists';
console.log('Origin string ======\n' +str_ex);
str_ex = str_ex.replace('exists', 'is awesome');
console.log('Revising string ======\n' +str_ex);
//NUMBERS
console.log('---- NUMBERS ----');
var nums = 199;
var float = 1.34;
console.log('NUMBERS ======\nint :' +nums+ ' float: ' +float);
//ROUNDING NUMBERS 四捨五入
console.log('---- ROUNDING NUMBERS ----');
var roudUp = 1.5;
var rouded = Math.round(roudUp);
console.log('ROUNDING NUMBERS ======\nrounding numbers is : ' + rouded);
//Number to String
console.log('---- Number to String ----');
var n = 256;
n = n.toString(); // change to string
console.log('Number to String ======\nstring is : ' + n);
//ACCESSING ARRAY VALUES
console.log('---- ACCESSING ARRAY VALUES ----');
var pets = ['cat' , 'dog' , 'rat'];
console.log('pets[0] : ' + pets[0]);
//LOOPING THROUGH ARRAYS
console.log('---- LOOPING THROUGH ARRApets--');
for (var i = 0; i < pets.length; i++) {
pets[i] = pets[i] + 's';
console.log('pets[%d]: ' +pets[i] , i);
}
//OBJECTS
console.log('---- OBJECTS ----');
var pizza = {
toppings: ['cheese', 'sauce', 'pepperoni'],
crust: 'deep dish',
serves: 2
}
console.log(pizza.toppings[0] + pizza.serves + pizza.crust);
//OBJECT PROPERTIES
console.log('---- OBJECT PROPERTIES ----');
console.log(pizza['crust'] + pizza['serves'] + pizza['toppings']);
//FUNCTIONS & FUNCTION ARGUMENTS
console.log('---- FUNCTIONS & FUNCTION ARGUMENTS----');
function eat(food){
return food + ' tasted really good. ';
};
function double_num(x){
return x * 2;
};
console.log(eat("bananas") + double_num(2));
function double_arg(x , food){
console.log('I have ' + x * 2 + ' ' + food);;
};
double_arg(2,'eggs');

0 意見:
張貼留言