Home > Article > Web Front-end > What should you pay attention to when writing js components? (Method introduction)
This article brings you what you need to pay attention to when writing js components? (Method introduction) has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.
Note:
1. When writing components, you must use anonymous functions and closures to encapsulate them to prevent naming conflicts between different components.
$(function(){ (function(a){ alert(a) })("aaaab") })
2. Format
3. Passing parameters You can pass one parameter in and use json to send multiple
4. JS does not distinguish between single quotes and double quotes
5. js inheritance
6. Implement chain operation
When importing the package in the jsp page, be sure to write the absolute path
1. Create a new js component that gets the basePath
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" isELIgnored="false"%> <% String basePath = ""; if (request.getServerPort() != 80) { basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + "/" + request.getContextPath(); } else { basePath = request.getScheme() + "://" + request.getServerName() + request.getContextPath(); } pageContext.setAttribute("basePath", basePath); %>
2. Reference it in the jsp that needs to be used
<%@ include file="/WEB-INF/pages/common/basePath.jsp"%>
3. Call
<script type="text/javascript" src="${basePath}/js/jquery.min.js"></script>
Note:
1. The pageContext of page is used here
2. Referring to the jsp file is equivalent to copying it Go to the reference interface, so you can use the setAttribute of the page
Related recommendations:
JS method to implement encryption (with code)
javascript Three ways to modify element css style (code)
The above is the detailed content of What should you pay attention to when writing js components? (Method introduction). For more information, please follow other related articles on the PHP Chinese website!