Más contenido relacionado
La actualidad más candente (20)
나쁘지만 사용해야 하는 속성들
- 1. 나쁘지만 사용해야 하는 속성들
(자바스크립트 핵심가이드 173 ~ 179 Page)
kbjin86@gmail.com
- 2. 나쁘다, 그러나 사용해야 한다.
1.전역변수
2.Scope
3.세미콜론 자동삽입
4.예약어
5.유니코드
6.typeof
7.parseInt
8.+
9.부동소수점
10.NaN (Not A Number)
11.가짜배열
12.거짓인값들
13.hasOwnProperty
14.Object
- 3. 7.parseInt
• parseInt("16") // 16
• parseInt("16 byte") // 16 , 아쉬운점 :잉여문자를 알수 없다.
• parseInt("007") // 7
• parseInt("010") // 8
• parseInt("08") // 0 --> 8진수로 파싱
• parseInt("09") // 0 --> 8진수로 파싱
• parseInt("09",10) // 9 --> 10진수로 파싱
• + “1” = 0 + parseInt(“1”)
기수 매개변수를 사용하는 습관을 들이자.
- 4. 8.+
• 숫자인경우만 덧샘
• 하나라도 문자열이 있으면 모두 문자열로 변경된후 연산함.
선언 연산 결과
var nn1 = 1;
var nn2 = 2;
nn1+nn2 3
var aa1 = '44'; nn1+aa1 '144'
var ss = [88,89]; nn2+ss; '288,89'
var oo = {dd:5, ee:'text'}; nn2+oo '2[object Object]'
var null_val = null nn1+null_val 1
var undef_val nn1+undef_val NaN
var ff = function() {
return 1; }
nn1+ff '1function () { return 1; }„
nn1+ff() 2
- 5. 9.부동소수점
> var rr = 0.1
> var rr2 = 0.2
> rr+rr2
0.30000000000000004
> (rr*10 + rr2*10)/10
0.3
소수점 연산을 적당히 조절하라.
- 6. 10.NaN (Not A Number)
// typeof 연산자는 숫자와 NaN 을 구분하지 못함.
> var nan1 = NaN
> var nan2 = NaN
> typeof NaN
'number'
> typeof nan1
'number„
> nan1 === nan2
false
> nan1 == nan2
false
> 1+nan1
NaN
> '1'+nan1
'1NaN'
> parseInt("1")
1
> parseInt("ss")
NaN
- 7. 10-2. isNaN(), isFinete() and ..
isNaN() isFinete()
typeof(val)==='number'
&& isFinite(val)
> isNaN(0)
false
> isNaN('0')
false
> isNaN('ss')
true
> isNaN(NaN)
true
> isFinite(0)
true
> isFinite('0')
true
> isFinite('ss')
false
> isFinite(NaN)
false
> isNumber(0)
true
> isNumber('0')
false
> isNumber('ss')
false
> isNumber(NaN)
false
- 8. 11.가짜배열
• 진정한 배열이 없다. 그러나 사용이 쉽고,크기지정이 불필요.
• 성능은 쫌 나쁘다.
• typeof() 는 배열과 객체를 구분하지 못한다.
• arguments 배열은 배열이 아니다 : length 속성을 갖은 객체다.
> var arr = new Array(1,2,3); { 0:1,1:2, 2:3, length:3 }
> arr.propertyIsEnumerable(1);
true
> arr.propertyIsEnumerable(3);
false
> arr.propertyIsEnumerable('length');
false
> arr.hasOwnProperty('length');
true
- 9. 12.거짓인값들
• 0, NaN, "", false, null, undefined
false
• undefined, NaN 은 상수가 아니다,
전역변수이다, 즉 값을 바꿀수도 있다.
바꿔서 사용하지는 말자 …
- 10. 13.hasOwnProperty
• 연산자가 아니라 메쏘드이다. 즉 값을 바꿀수도 있다.
> var arr = [];
> arr.hasOwnProperty('length');
true
> arr.hasOwnProperty = function() { return false; }
[Function]
> arr.hasOwnProperty('length');
false
- 11. 14. 객체
var i, var word;
var text = "one two two three constructor";
var words = text.split(" ");
var count = {};
for ( i=0; i<words.length; i++) {
word = words[i] ;
if(count[word]) count[word] += 1;
else count[word] = 1;
}
for ( i=0; i<words.length; i++) {
console.log("["+words[i]+"] "+i+"/"+words.length);
}
for( i in count) {
console.log("--> count["+i+"] : ["+count[i]+"]");
}
>> words[]
[one] 0/5
[two] 1/5
[two] 2/5
[three] 3/5
[constructor] 4/5
>> count{}
--> count[one] : [1]
--> count[two] : [2]
--> count[three] : [1]
--> count[constructor] : [function Obje
ct() { [native code] }1]
- 12. 14. 객체 - 2
var count2 = {};
for ( i=0; i<words.length; i++) {
word = words[i] ;
if(count2[word] && typeof(count2[word])==='number' ) {
count2[word] += 1;
}
else count2[word] = 1;
}
for( i in count2) {
console.log("--> count2["+i+"] : ["+count2[i]+"]");
}
--> count2[one] : [1]
--> count2[two] : [2]
--> count2[three] : [1]
--> count2[constructor] : [1]
// var count2 = { 'construuctor' : function() {.. ,} }
// count2 객체는 Object.prototype을 상속받았다, 여기에 constructor 속성이 있다.