Skip to content

Instantly share code, notes, and snippets.

@jonnung
Last active August 29, 2015 14:07
Show Gist options
  • Save jonnung/565c013ffc5e44c82ea9 to your computer and use it in GitHub Desktop.
Save jonnung/565c013ffc5e44c82ea9 to your computer and use it in GitHub Desktop.
Javascript the good parts - Function #2 (Simplex Internet JS study group)
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('숫자가 아니잖아!!');
}
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);
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