Coding Feature.

JS) 코딩하다가 알게 된 사실. (다른 javascript 파일의 함수 사용) 본문

Programming Language/JavaScript

JS) 코딩하다가 알게 된 사실. (다른 javascript 파일의 함수 사용)

codingfeature 2023. 1. 13. 02:41

A function cannot be called unless it was defined in the same file or one loaded before the attempt to call it.

A function cannot be called unless it is in the same or greater scope then the one trying to call it.

You declare function fn1 in first.js, and then in second you can just have fn1();

1.js:

function fn1 () {
    alert();
}

2.js:

fn1();

index.html :

<script type="text/javascript" src="1.js"></script>
<script type="text/javascript" src="2.js"></script>