Constructor : The function for creating object, object creator.
this : Within the function, it indicates the object that function belong to. The object which creator created.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<script>
var person = {} //object always can be made with {}, person is variable
person.name='aaa';//put the name varialbe into the object container. Normally the variable contained in the objext is called a property
person.introduce=function(){//another property which name is introduce. It contains method(function)
return 'my name is '+this.name;
}
document.write(person.introduce());
function Person(name){
this.name=name;
this.introduce=function(){//only one time defines the method here
return this.name;
}
}
var p1=new Person('abc');//Person is a object(creator)
document.write(p1.introduce());
var p2=new Person('def');
document.write(p2.introduce);
function func(){
alert('hello');
}
func();
window.func();//window is an object, func is a property, () is callable, meaning function, therefore we can call this property as a method
//In Javascrip, all objects are basically property of global object
//window is a client javascript, to be precise, local object of web browser javascript
//normarlly, window is ommitted
var o={}
var p={}
function func(){
switch(this){//this indicates window
case o:
document.write('o');
break;
case p:
document.write('p');
break;
case window:
document.write('window');
break;
}
}
func();
func.apply(o);
func.apply(p);
</script>
</body>
</html>
'JavaScript' 카테고리의 다른 글
prototype, random (0) | 2021.01.12 |
---|---|
object, encapsulation, information hiding, interface (0) | 2021.01.11 |
arguments, function (0) | 2021.01.11 |
ajax, closure (0) | 2021.01.11 |
global variable, anonymous function (0) | 2021.01.07 |