2014-10-24 5 views
-2

다음 코드에서 실행하면 경고를 발생시키는 click 이벤트를 기다립니다. 내가JavaScript에서 두 가지 다른 함수 호출 방법

에서 다음 자바 스크립트 코드

을 변경하지만 경우

clickMeButton.onclick = runTheExample;

clickMeButton.onclick runTheExample =();

클릭 이벤트없이 페이지를 다운로드 할 때 항상 경고가 발생합니다. 차이점을 알고 싶습니다. Chrome을 사용 중입니다. 냈다 코드는 다음과 같습니다

<!DOCTYPE html> 
<html> 
<head> 
<title>DOM Example</title> 
    <script type="text/javascript" src="script13.js"> </script> 
</head> 

<body> 
    <h1 id="title">DOM Example </h1> 
    <p id="first">This is the first paragraph</p> 
    <p id="second"><strong>This is the second paragraph</strong></p> 
    <p id="third">This is the third paragraph</p> 
    <input type ="text" id="myTextBox" /> 
    <input type ="submit" id="clickMe" value="Click Me!"/> 
    <a href="http://www.google.com" id="MyGoogle"> Google! </a> 
</body> 
</html> 

//script13.js 
window.onload= function(){ 
var clickMeButton=document.getElementById('clickMe'); 
clickMeButton.onclick=runTheExample; 
} 

function runTheExample(){ 
alert('running the example'); 
} 
+1

당신이 VAR X = FUNC를() 당신이 기능을 실행하고 기능 실행의 VAR X = 수익을하고 있습니다 호출 할 때 나는 생각한다. 그리고 var = func을 호출하면 var x가 이제는 함수라는 것을 알 수 있습니다. –

+0

두 번째 표기법에서'clickMeButton.onclick'은'runTheExample()'의 ** 값 반환 **에 할당되는 반면, ** 참조 **는 함수 ('runTheExample')에 대한 것입니다 – hindmost

+0

고마워, 포인트. – casillas

답변

3
var x = func(); //Means you execute the function and output its return value to x; 

var x = func; //Means you attribute the function to the variable, so you can call it, using x. 

function f(){ 
return 4; 
} 

var x = f(); // x = 4 
var x = f; // x = f() 
+0

감사합니다 Diogo, 지금 제가 요점을 가지고 있습니다. SFO가 10 분 안에 나에게 허락하면 나는 대답으로 표시 할 것이다. – casillas