JavaScript 总结

March 2017 3 minute read

函数(function)

var add = function (a, b) {
    if (typeof a !== 'number' || typeof b !== 'number') {
        throw {
            name: 'TypeError',
            message: 'add needs numbers'
    }
}
    return a + b;
}
var factorial = function factorial(i, a) {
    a = a || 1;
    document.writeln(i,':',a);
    if (i < 2) {
    return a;
    }
    return factorial(i - 1, a * i);
    };
document.writeln(factorial(4));
var b=5;
(b == 5) ? a="true" : a="false";
document.writeln(a)
var myObject = function(){
    var value = 0 ;
    return {
        increment:function(inc){
            value += typeof inc === 'number' ? inc :1;
        },
        getvalue:function(){
            return value;
        }
    }
}();
var fade = function(node){
    var level = 1;
    var step = function(){
        var hex = level.toString(16);
        node.style.backgroundColor = '#FFFF'+hex+hex;
        if (level<20){
            level += 1
            setTimeout(step,100)
    }
};
setTimeout(step,100)
};
fade(document.body)
var fibonacci = function(){
    var memo = [0,1];
    var fib = function(n){
        var result = memo[n];
        if (typeof result !== 'number'){
            result = fib(n-1) + fib(n-2);
            memo[n] = result;
        }
        return result;
    }
    return fib;
}();
for (var i = 0; i <= 10; i += 1) {
    document.writeln('// ' + i + ': ' + fibonacci(i));
    }

Array

var  numbers = [1,2,3];
numbers.length
numbers[0]
numbers[numbers.length] = 'shi';
numbers.push('go');
delete numbers[2]
// // JavaScript arrays have a splice method. It can do surgery on an array, deleting some number
// // of elements and replacing them with other elements. The first argument is an ordinal in the array. The second
// // argument is the number of elements to delete. Any additional arguments get inserted into the array at that
// point:
numbers.splice(2, 1);
myArray = ['apple','banana','orange']
var i;
for (i = 0; i < myArray.length; i += 1) {
document.writeln(myArray[i]);
}
Array.prototype.test = function(){
    return this
}
document.writeln(numbers.test())
numbers = [1,2,3,4]
var add = function(a,b){
    return a + b
}
myArray.total = function(){return this.reduce(add)}
document.writeln(numbers.total())

正则表达式(regular expression)

var parse_url = /^(?:([A-Za-z]+):)?(\/{0,3})([0-9.\-A-Za-z]+)(?::(\d+))?(?:\/([^?#]*))?(?:\?([^#]*))?(?:#(.*))?$/;
var url = "http://www.ora.com:80/goodparts?q#fragment";
var result = parse_url.exec(url);
var names = ['url', 'scheme', 'slash', 'host', 'port',
'path', 'query', 'hash'];
var blanks = ' ';
var i;
for (i = 0; i < names.length; i += 1) {
document.writeln(names[i] + ':' +
blanks.substring(names[i].length), result[i]);
}
for (i = 0; i < names.length; i += 1) {
document.writeln(names[i] + ':' +
blanks.substring(names[i].length), result[i]);
}
var parser_number = /^-?\d+(?:\.\d*)?(?:e[+\-]?\d+)?$/i;
var test = function(num){
    document.writeln(parser_number.test(num))
}
test('1'); // true
test('number'); // false
test('98.6'); // true
test('132.21.86.100'); // false
test('123.45E-67'); // true
test('123.45D-67'); // false