html 요소에서 사용되는 경우 this
은 DOM의 요소를 참조합니다.
예 : this
비 복귀 기능에서 사용된다
<script type="text/javascript">
function hover(domElement) {
domElement.style.backgroundColor = '#CCC';
}
</script>
<div onmouseover="hover(this);">Hover me</div>
이 함수는 객체의 생성자되고, this
함수가 호출 될 때 자동 복귀 도착 내재적 생성 된 객체를 의미 new
.
예 :
function Point(x, y) {
this.x = x || 0;
this.y = y || 0;
this.distance = function (point) {
return Math.sqrt(
Math.pow(point.x - this.x, 2) +
Math.pow(point.y - this.y, 2)
);
};
}
var a = new Point(10, 10);
var b = new Point(100, 100);
document.write(a.distance(b));
this
및 new
없이 동일한 기능 :
function Point(x, y) {
obj = {};
obj.x = x || 0;
obj.y = y || 0;
obj.distance = function (point) {
return Math.sqrt(
Math.pow(point.x - this.x, 2) +
Math.pow(point.y - this.y, 2)
);
};
return obj;
}
var a = Point(10, 10);
var b = Point(100, 100);
document.write(a.distance(b));
당신이 구글에서 '이 자바 스크립트 "를 검색 시도? – elclanrs
이것은 매우 큰 문제에 대해서는 매우 작은 질문입니다. 당신은 문제를 해결하기 위해 몇 가지 사례를 시도해야합니다. 아무도이 문제에 대한 답변을 제공하지 않습니다. – Eraden
그것은'VanillaBean'이 어떻게 불려지 는가에 달려 있지만 아마도'new', 즉 [생성자 함수] (https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide)와 함께 호출됩니다./Working_with_Objects # Using_a_constructor_function). 이것은 또한 도움이 될 수 있습니다 : [객체 지향 자바 스크립트 소개] (https://developer.mozilla.org/en-US/docs/Web/JavaScript/Introduction_to_Object-Oriented_JavaScript) –