Last active
November 3, 2016 09:32
-
-
Save webbingstudio/891fe60cf75d0068791b6daddc4514fc to your computer and use it in GitHub Desktop.
JavaScriptの関数の引数が想定していない値ならデフォルト値を強制する(つっこみ待ち) ref: http://qiita.com/webbingstudio@github/items/6e7a77ed2347a59efedd
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var example = function( current, value ){ | |
changed = Number(current) + Number(value); | |
// changedで何かする | |
} | |
$('a.js-example').click(function(){ | |
var current = docCookies.getItem('current'); | |
var value = $(this).data('value'); | |
example( current, value ); | |
return false; | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var default_value = function( val, default_val, nan_check ){ | |
var return_val = val == undefined ? default_val : val; | |
return_val = val === null ? default_val : val; | |
if( nan_check === 1 ) { | |
return_val = isNaN(val) ? default_val : val; | |
} | |
return return_val; | |
}; | |
var example = function( cur, val ){ | |
var current = default_value( cur, 0, 1 ); | |
var value = default_value( val, 0, 1 ); | |
changed = Number(current) + Number(value); | |
// changedで何かする | |
} | |
$('a.js-example').click(function(){ | |
var current = docCookies.getItem('current'); | |
var value = $(this).data('value'); | |
example( current, value ); | |
return false; | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment