Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- (function(){
- var define = function( variable, value ){
- // variable が false, または値が不正なとき false を返す.
- if( !variable || variable.match(/[^A-Z|^a-z|^0-9]/g) ) return false;
- else{
- // value が存在する場合.
- if( value ){
- // this.第1引数(variable) の値を受け取る.
- var func = (new Function( 'return this.' + variable ))();
- if( func === undefined ){
- var type = (typeof value);
- // value が文字列.
- if( type === 'string' ) (new Function( 'this.'+variable+'="'+value+'";' ))();
- // value が文字列ではない.
- else (new Function( 'this.'+variable+'='+value+';' ))();
- // 既に使用されている場合.
- }else return false;
- }else if( !value ){
- // this.第1引数(variable) の値を返す.
- return (new Function( 'return this.' + variable ))();
- // 例外処理.
- }else return false;
- }
- // 代入完了.
- return true;
- };
- alert(define('abc','text')); // true
- alert(define('abc','abcd')); // false
- alert(define('abc')); // 'text'
- window.define = new define;
- })();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement