티스토리 뷰

* 함수의 사용

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." );

'웹개발 > Javascript' 카테고리의 다른 글

자바스크립트 With Statements  (0) 2010.09.10
자바스크립트 정규식 예제  (0) 2010.09.10
템플릿 메서드 패턴  (0) 2010.07.15
커맨더 패턴 ( Command Pattern )  (0) 2010.07.13
자바스크립트 Observer 패턴 예제  (1) 2010.07.05
댓글
D-DAY
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
«   2024/11   »
1 2
3 4 5 6 7 8 9
10 11 12 13 14 15 16
17 18 19 20 21 22 23
24 25 26 27 28 29 30
글 보관함