Last active
August 29, 2015 14:07
-
-
Save jonnung/565c013ffc5e44c82ea9 to your computer and use it in GitHub Desktop.
Javascript the good parts - Function #2 (Simplex Internet JS study group)
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 myObject = { | |
value: 1, | |
increment: function (inc) { | |
var inc_type = typeof inc; | |
var that = this; | |
var minus = function () { | |
return that.value - 5; | |
}; | |
this.value += (inc_type === 'number')? inc : 1; | |
if (inc_type === 'string') { | |
return false; | |
} else { | |
return minus(); | |
} | |
} | |
}; | |
var result = myObject.increment(10) | |
if (result) { | |
console.log(result); | |
} else { | |
console.log('숫자가 아니잖아!!'); | |
} |
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 myObject = { | |
value: 1, | |
increment: function (inc) { | |
try { | |
var that = this; | |
var minus = function () { | |
return that.value - 5; | |
}; | |
if (typeof inc !== 'number') { | |
throw new Error('숫자가 아니잖아!!'); | |
} | |
if (inc < 4) { | |
throw new Error('4보다 큰 숫자만 계산 할 수 있음'); | |
} | |
this.value += inc; | |
return minus(); | |
} catch (e) { | |
console.log(e.message); | |
} | |
} | |
}; | |
var result = myObject.increment(3); | |
console.log(result); |
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 myObject = { | |
value: 1, | |
increment: function (inc) { | |
try { | |
var that = this; | |
var minus = function () { | |
return that.value - 5; | |
}; | |
if (typeof inc !== 'number') { | |
throw new Error('숫자가 아니잖아!!'); | |
} | |
if (inc < 4) { | |
throw new Error('4보다 큰 숫자만 계산 할 수 있음'); | |
} | |
this.value += inc; | |
return minus(); | |
} catch (e) { | |
console.log(e.message); | |
} | |
} | |
}; | |
Function.prototype.method = function (name, func) { | |
this.prototype[name] = func; | |
} | |
Number.method('to_integer', function () { | |
return Math[(this < 0)? 'ceil' : 'floor'](this); | |
}); | |
var result = myObject.increment(10.54545423); | |
console.log(result); | |
console.log(result.to_integer()); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment