Home > Web Front-end > JS Tutorial > body text

How to extract public functions from applet to util.js

零到壹度
Release: 2018-04-14 14:47:26
Original
1751 people have browsed it

The content of this article is to share with you how to extract public functions from small programs to util.js. It has certain reference value. Friends in need can refer to it

In In the mini program, a tool file utils is defined. The js of this file is intended to be valid within this file. When other sub-pages want to call the js methods or variables in it, two steps are required:

1: In the js file called by utils, the object-oriented model output: module.exports={Name of the function to be called: Name of the function to be called};

2: In the Modularly introduce the js file of utils in the called js file var object=require("the js file address where utils is called"); You can output the object to see the called method;

Examples are as follows:

js called in utils:

var URl='http://123.23.169';
 var getImageurl=function(imageurl){
return URl+imageurl;
 }
 
//  要引用这个文件的函数或者变量,除了在要引用的的js文件中模块化之外(var utils=require('js地址')),
// 在被引用的的js中要通过 module.exports={a:a}作为面向对象的变量输出函数如下:
 module.exports={
     URl:URl,//要引用的函数 xx:xx
     getImageurl:getImageurl
 }
Copy after login

js file to be called :

// 获得工具utils工具js里面函数,先模块化引用utils里面的js地址  reqiure('js地址')成一个面向对象
var utils=require('../../utils/app.js')
// console.log(utils) 可查看获得的函数
console.log(utils.getImageurl('iamgeaaddress.png'))  
Copy after login

Output result:


#Related recommendations:

Extract util public method

Text extraction

Little program The public js is extracted to the instance called in app.js

The above is the detailed content of How to extract public functions from applet to util.js. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!