代码实践

闭包举例:

function closure(){
    var innerVariable = 'inner';
    function innerFn(){    
        return innerVariable;
    }
    return innerFn();
}
var innerVariable = 'defined in outer';
var outerVariable = closure();
console.log(outerVariable);

测试字符串是否回文:

function testString(testString){
    var length = testString.length;
    for(var i = 0; i <= (length-1)/2;i++){
        if(testString[i] !== testString[length - 1 - i]){
            return false;
        }
    }
    return true;
}

 

var isPalindrome = function(testString){
    var length = testString.length;
    var splittedArray = testString.split('');
    return splittedArray.join() == splittedArray.reverse().join();
}

console.log(isPalindrome('1221'));
console.log(isPalindrome('1344'))

 

var isPalindrome = function(testString){
    var length = testString.length;
    for(var i = length - 1,j=0; i >= 0;i--){
     if(i <= j){
      return true;
     }
if(testString[i] == testString[j++]){ continue; }else{ return false; } } return true; } console.log(isPalindrome('121')) console.log(isPalindrome('1221')) console.log(isPalindrome('1234'))

 

function isPalindrome(testString){
    var reversedString = '';
    for(var i = testString.length; i >= 0; i-- ){
        reversedString += testString.charAt(i);
    }
    if(reversedString == testString){
        return true;
    }else{
        return false;
    }
}

console.log(isPalindrome('121'));
console.log(isPalindrome('1213'));

 

数组去重:

function uniqueArray(array){
var isArray = function(array){
var isArray = Object.prototype.toString.call(array) == "[object Array]";
if(isArray){
console.log(array + ' is an array');
}
else{
console.log(array + ' is not an array');
}
return isArray;
}
if(!!isArray(array)){
var length = array.length;
for(var i = length - 1; i >=0; i--){
if(i == 0){
continue;
}
for(var j = i - 1; j>=0;j--){
if(array[j] == array[i]){
console.log(array[j]);
array.splice(j,1);
}
console.log(array)
}
}
console.log(array);
return array;
}else{
return null;
}
}

var testArray = [12,123,34,12];
testArray = uniqueArray(testArray);
console.log(testArray)

 

 

Curry Function:

function curry(fn,arg1,arg2){
var toArray = function(args,n){return Array.prototype.slice.call(args,n);}
var requiredArgsLength = fn.length;
console.log('requiredArgsLength:' + requiredArgsLength);
if((arguments.length - 1) < requiredArgsLength){
console.log('lack of arguments'); 
var partialArgs = toArray(arguments,1);
console.log(partialArgs);
return function(){
var args = partialArgs.concat(toArray(arguments,0));
console.log('completeArgs:' + args);
return fn.apply(null,args);
}
}
else{
return fn.apply(null,toArray(arguments,1));

}
}

function add(arg1,arg2){
return arg1 + arg2;
}
console.log(curry(add,4,5))
var curriedFn = curry(add,4);
curriedFn(5)

 

 

Fibnacci:

function fib(n){if(n==1 || n ==0) {return n;} else{return n*fib(n-1)}}

 

//indexof test

var testNum = 9;

~testNum + 1 == -testNum;

 

 

查找特定tag:

function tagCounter(tagName){
switch(tagName.toLowerCase()){
case 'p': return document.getElementsByTagName('p');
case 'a': return document.getElementsByTagName('a');
}
}

实现继承:

var parent = function(){this.parentName = 'parent';}
var child = function(){this.childName = 'child'}
child.prototype = new parent();
var childInstance = new child();
childInstance.childName
childInstance.parentName
childInstance.__proto__ instanceof parent

 

闭包:


function outerFunction(){
var innerValue = 10;
return function(){
return innerValue;
}
}

var outerValue = outerFunction()();
console.log(outerValue);

利用构造函数实现继承:

var parentObject = {parentName:'parent'}
var childObject = {childName: 'child'}
var tempFunction = function(){}
tempFunction.prototype = parentObject
childObject = new tempFunction
childObject.parentName

比较对象是否相等:

function isEqualObject(src,obj){
var srcJSONString = JSON.stringify(src)
var objJSONString = JSON.stringify(obj)
if(!( (/:/.test(srcJSONString)).length == (/:/.test(objJSONString)).length){return false;}
if(keys(src) != keys(obj)){
return false;
}
for(var i in src){ if(src[i] == obj[i]){ continue;} else{return false}} return true;
}

function keys(obj){
var keysArray = [];
for(var i in obj){
if(obj.hasOwnProperty(i)){
keysArray.push(i)
}
else{continue;}
}
}

underscorejs自己实现:

function delay(fn,milliseconds){setTimeout(fn,milliseconds)}

多态:

function multiple(){
if(arguments.length == 0){
console.log('please provide params');
return false;
}
else if(arguments.length == 1){
return arguments[0]
}
else if(arguments.length == 2){
return arguments[0]+ arguments[1]
}
}

console.log(multiple())
console.log(multiple(2))
console.log(multiple(2,3))

 

throttled Version:

var clickHandler = function(e){console.log(e);console.log(new Date())}

var throttledClickHandler = _.throttle(clickHandler,2000)

window.addEventListener('click',throttledClickHandler)

 

debounce version:

var clickHandler = function(e){console.log(new Date())}

var debouncedClickHandler = _.debounce(clickHandler,2000)

window.addEventListener('click',debouncedClickHandler)

 

extend example:

 

var testPropertyObject = {name:'test'}
var targetObject = {testProperty:testPropertyObject}
var destinationObject = _.extend(targetObject,{extendProperty:'extended'})
destinationObject.testProperty == testPropertyObject
destinationObject.testProperty === testPropertyObject

绑定多个事件:

$('.noresult').on({'click':function(e){console.log('click')},'touchstart':function(e){console.log('touchStart')}})

 

取消多个事件的绑定:

$('.noresult').on('touchstart',function(e){console.log('touchstart')})

$('.noresult').on('click',function(e){console.log('click')})

$('.noresult').off(['click','touchstart'])

posted on 2015-03-10 10:15  跳雀  阅读(437)  评论(0编辑  收藏  举报