dev/JavaScript

여러가지 객체사용법 2

아디봉 2017. 4. 27. 15:49

여러가지 객체 사용법 2


 

* 아래 내용은 공부용으로 찾아보고 만든 코드이니.. 참고만 하시길 바랍니다.

  추가 정리중...

  


배열에 대한..


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
var arr = [], //new Array() 와 동일 하지만 var arr 은 사용하지말라
        a = [],            // these are the same
    b = new Array();   // a and b are arrays with length 0
    
var c = ['foo''bar'],           // these are the same
= new Array('foo''bar');  // c and d are arrays with 2 strings
    // these are different:
    
 var e = [3], // e.length == 1, e[0] == 3
         f = new Array(3);   // f.length == 3, f[0] == undefined
console.log('e.length ' + e.length + 'e[0] ' + e[0] );
console.log('f.length ' + f.length + 'f[0] ' + f[0] );
 
===== 
결과값 
e.length = 1 e[0= 3
f.length = 3 f[0= undefined
=====
cs


this 의 쓰임


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
//this의 의미 => this는 해당 instance를 function 형 그 자체를 의미합니다.
//this의 쓰임 (1)
function Company2() {
    this.name = "Company2 name";
    this.AlertName = AlertName2;
}
function Employee(){
    this.name = "Employee name";
    this.AlertName = AlertName2;
}
function AlertName2(){
    console.log(this.name);
}
(new Company2).AlertName();
(new Employee).AlertName();
 
// this의 쓰임(2)
function Company3(){
    this.CompanyName = "Company3 CompanyName";
    this.AlertCompanyName = function(){
        console.log(this.name);
    }
}
Department2.prototype = new Company3();
Department2.constructor = Department2;
function Department2(){
    this.DepartmentName = "Department2 DepartmentName";
    this.AlertDepartmentName = function(){
        console.log(this.CompanyName + " > "+ this.DepartmentName);
    }
}
(new Department2).AlertDepartmentName();
 
===== 
결과값 
Company2 name
Employee name
Company3 CompanyName > Department2 DepartmentName
=====
 
cs