WINEDT Skills Organization

2023-01-23   ES  

This problem is a way I use at work because I need to pack
So think of

CanCan you reference to the function in another JS in one JS?
can greatly reduce the number of code if this can be greatly reduced

First introduce two js files on the HTML page
这里写图片描述
1.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title> </title>
</head>
<script src="1.js"></script>
<script src="2.js"></script>
<body>

</body>
</html>

1.js
这里写图片描述

function a(){
    
    alert(1);
}

2.js
这里写图片描述

a();

This way 2.JS can directly call the A function in 1.JS
Of course, this is just a very simple way. Write a method of closing (of course it is also very simple ~~)
1.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title> </title>
</head>
<script src="1.js"></script>
<script src="2.js"></script>
<body>

</body>
</html>

1.js
这里写图片描述

var dianji;
(function(){
    
dianji = {
        a:function(){
    
            alert(1);
        }
}
})()

Here 1.JS is an object function and a closure form, so we need to raise the Dianji variable to the global. In this way, this A function can be called in 2.JS, otherwise the error of the reward variable is not defined.

If you want to call the function between JS, the function must be global

2.js
这里写图片描述

dianji.a();

source

Related Posts

ROS2 minimalist summary-core concept (2)

How to connect the Raspberry Pi gateway to the built -in LORAWAN Network Server -teach you how to connect the Raspberry Pi gateway to the 5th article of the server

bzoj2342 shoi2011 double back text [Manacher] Dreammaker

[Twelve Provincial Joint Examination 2019] The Twelve Rings of the Spring Festival

WINEDT Skills Organization

Random Posts

UNI-APP and Bluetooth device transmission

How to upgrade the PHP of MacOS to the designated version (5.3-7.3)

libevent Example: Hello-free

Enhanced for loop

LOGSTASH nested json/multi -layer JSON parsing configuration configuration zhcha