티스토리 뷰

* 함수의 사용

function isNimble(){ return true; }
var canFly = function(){ return true; };
window.isDeadly = function(){ return true; };

//함내의 함수 
function stealthCheck()
{
    var ret = stealth() == stealth();
    assert( ret, "함수함수!" )
    return true;
    function stealth(){ return true; }
}

stealthCheck();

var obj = {};
var fn = function(){};

obj.prop = "some value";

fn.prop  = "some value";
assert( obj.prop == fn.prop,"Both are objects, both have the property." );

var store = {
    id: 1,
    cache: {},
    add: function( fn ) {
        if ( !fn.id ) {
            fn.id = store.id++;
            return !!(store.cache[fn.id] = fn);
        }
    }
};

function ninja(){}
assert( store.add( ninja ), "Function was safely added." );
assert( !store.add( ninja ), "But it was only added once." );


var object = {};
function fn(){
    return this;
}

assert( fn() == this, "The context is the global object." );
assert( fn.call(object) == object,"The context is changed to a specific object." );

function add(a, b){
    return a + b;
}

assert( add.call(this, 1, 2) == 3,".call() takes individual arguments" );
assert( add.apply(this, [1, 2]) == 3,".apply() takes an array of arguments" );

function loop(array, fn){
    for ( var i = 0; i < array.length; i++ ) {
        if ( fn.call( array, array[i], i ) === false )
        break;
    }
}

var num = 0;
loop([0, 1, 2], function(value, i){
    assert(value == num++,"Make sure the contents are as we expect it.");
});

function smallest(array){
    return Math.min.apply( Math, array );
}
function largest(array){
    return Math.max.apply( Math, array );
}
assert(smallest([0, 1, 2, 3]) == 0, "Locate the smallest value.");
assert(largest([0, 1, 2, 3]) == 3, "Locate the largest value.");

function merge(root){
    for ( var i = 1; i < arguments.length; i++ )
    for ( var key in arguments[i] ) {
        root[key] = arguments[i][key];
    }
    return root;
}
var merged = merge({name: "John"}, {city: "Boston"});
assert( merged.name == "John", "The original name is intact." );
assert( merged.city == "Boston","And the city has een copied over." );

Timer

var timers = {
    timerID: 0,
    timers: [],
        start: function(){
            if ( this.timerID )
                return;
            (function(){
                for ( var i = 0; i < timers.timers.length; i++ )
                    if ( timers.timers[i]() === false ) {
                        timers.timers.splice(i, 1);
                        i--;
                    }
                timers.timerID = setTimeout( arguments.callee, 0 );
            })();
        },
        stop: function(){
        clearTimeout( this.timerID );
            this.timerID = 0;
        },
        add: function(fn){
            this.timers.push( fn );
            this.start();
        }
};

var box = document.getElementById("box"), left = 0, top = 20;

timers.add(function(){
    box.style.left = left + "px";
    if ( ++left > 50 )  return false;
});

timers.add(function(){
    box.style.top = top + "px";
    top += 2;
    if ( top > 120 ) return false;
});


정규 표현식

var re = /test/i;
var re2 = new RegExp("test", "i");

assert( re.toString() == "/test/i","Verify the contents of the expression." );
assert( re.test( "TesT" ), "Make sure the expression work." );
assert( re2.test( "TesT" ), "Make sure the expression work." );
assert( re.toString() == re2.toString(),"The contents of the expressions are equal." );
assert( re != re2, "But they are different objects." );

function find(className, type) { var elems = document.getElementsByTagName(type || "*"); var re = new RegExp("(^|\\s)" + className + "(\\s|$)"); var results = []; for ( var i = 0, length = elems.length; i < length; i++ ) { if ( re.test( elems[i].className ) ) results.push( elems[i] ); } return results; } assert( find("ninja", "div").length == 3,"Verify the right amount was found." ); assert( find("ninja").length == 3,"Verify the right amount was found." ); function RegMerge() { var expr = []; for ( var i = 0; i < arguments.length; i++ ) expr.push( arguments[i].toString().replace(/^\/|\/\w*$/g, "") ); return new RegExp( "(?:" + expr.join("|") + ")" ); } var re = RegMerge( /Ninj(a|itsu)/, /Sword/, /Katana/ ); assert( re.test( "Ninjitsu" ),"Verify that the new expression works." ); assert( re.test( "Katana" ),"Verify that the new expression works." );
저작자 표시
신고
댓글
댓글쓰기 폼