Advertisement
Guest User

Untitled

a guest
Dec 18th, 2014
155
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.27 KB | None | 0 0
  1. (function(){
  2.  
  3. var define = function( variable, value ){
  4.  
  5. // variable が false, または値が不正なとき false を返す.
  6. if( !variable || variable.match(/[^A-Z|^a-z|^0-9]/g) ) return false;
  7. else{
  8.  
  9. // value が存在する場合.
  10. if( value ){
  11.  
  12. // this.第1引数(variable) の値を受け取る.
  13. var func = (new Function( 'return this.' + variable ))();
  14. if( func === undefined ){
  15.  
  16. var type = (typeof value);
  17. // value が文字列.
  18. if( type === 'string' ) (new Function( 'this.'+variable+'="'+value+'";' ))();
  19. // value が文字列ではない.
  20. else (new Function( 'this.'+variable+'='+value+';' ))();
  21.  
  22. // 既に使用されている場合.
  23. }else return false;
  24.  
  25. }else if( !value ){
  26.  
  27. // this.第1引数(variable) の値を返す.
  28. return (new Function( 'return this.' + variable ))();
  29.  
  30. // 例外処理.
  31. }else return false;
  32.  
  33. }
  34. // 代入完了.
  35. return true;
  36.  
  37. };
  38.  
  39. alert(define('abc','text')); // true
  40. alert(define('abc','abcd')); // false
  41. alert(define('abc')); // 'text'
  42.  
  43. window.define = new define;
  44.  
  45. })();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement