博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
代码实践
阅读量:5124 次
发布时间:2019-06-13

本文共 5581 字,大约阅读时间需要 18 分钟。

闭包举例:

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.childNamechildInstance.parentNamechildInstance.__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'])

转载于:https://www.cnblogs.com/social/p/4325226.html

你可能感兴趣的文章
django迁移数据库错误
查看>>
Data truncation: Out of range value for column 'Quality' at row 1
查看>>
字符串处理
查看>>
HtmlUnitDriver 网页内容动态抓取
查看>>
ad logon hour
查看>>
罗马数字与阿拉伯数字转换
查看>>
Eclipse 反编译之 JadClipse
查看>>
Python入门-函数
查看>>
距离公式汇总以及Python实现
查看>>
Linux内核态、用户态简介与IntelCPU特权级别--Ring0-3
查看>>
第23月第24天 git命令 .git-credentials git rm --cached git stash clear
查看>>
java SE :标准输入/输出
查看>>
[ JAVA编程 ] double类型计算精度丢失问题及解决方法
查看>>
好玩的-记最近玩的几个经典ipad ios游戏
查看>>
PyQt5--EventSender
查看>>
Sql Server 中由数字转换为指定长度的字符串
查看>>
tmux的简单快捷键
查看>>
[Swift]LeetCode922.按奇偶排序数组 II | Sort Array By Parity II
查看>>
Android打包key密码丢失找回
查看>>
VC6.0调试技巧(一)(转)
查看>>