JavaScript的prototype的使用(以ellipsis函数为例子)

本文介绍如何在JavaScript中实现字符串截取功能,并通过prototype技术优化代码效率。包括基本函数实现、调用方法和使用场景展示。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1、一般使用JavaScript函数时,若遇到没有JavaScript的原始函数时,都会自己写一个函数,然后再调用。例如:

在一个数据列表中的一个单元格中显示数据,当要显示的数据长度超过20 位字符串则截取,字符后加。。。

下面去实现:

写一个截取的函数:

/**
 * <pre>
 * 截取一个字符串,若它超过指定的长度,并在其后面增加一个省略号('...')
 * </pre>
 * @param value 需要截取的字符串
 * @param len 允许截取的最大长度
 * @returns 返回转换后的字符串
 */
function ellipsis(value, len){
	if(value && value.length > len){
        return value.substr(0, len-3)+"...";
    }
    return value;
}


在前台界面调用它。

<%@ page language="java" import="java.util.*" pageEncoding="GBK"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>My JSP 'index.jsp' starting page</title>
	<meta http-equiv="pragma" content="no-cache">
	<meta http-equiv="cache-control" content="no-cache">
	<meta http-equiv="expires" content="0">    
	<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
	<meta http-equiv="description" content="This is my page">
	<!--
	<link rel="stylesheet" type="text/css" href="styles.css">
	-->
	<link rel="stylesheet" type="text/css" href="./css/ext-all.css" />
	<!-- 导入写好的函数 -->
	<script type="text/javascript" src="./js/format.js"></script>
	<script type="text/javascript">
		function ddd(){
		 var aaa = "GWT-Ext does not distribute ExtJS. Note that GWT-Ext only supports";
		
		var bbb = ellipsis(aaa,20);
		alert(bbb);
		
		/*var ccc = ellipsis(aaa,500);
		alert(ccc);
		
		Ext.onReady(function(){
			var eee = Ext.util.Format.ellipsis(aaa,20);
			Ext.MessageBox.alert("xxx",eee);
			
		}); */
			//var aaa = "GWT-Ext does not distribute ExtJS. Note that GWT-Ext only supports";
			//alert(aaa.ellipsis(20));
		}
		

	</script>
  </head>
  
  <body onload="ddd();">
    This is my JSP page. <br>
    <input type="button" value="showWindow" name="showWindow" onclick="showForm(this);" title="showWindow"></input>
  </body>
</html>


 

看运行结果:

 

 

结果正确。

如果我们想换一种方式,以“.”运算符去调用呢?又该如何实现呢。?

那么我们就要使用到prototype原型了。至于prototype是一个什么意思呢?在这里不作解释。

 

JavaScript代码:

/**
 * <pre>
 * 使用String的prototype的形式,即可以使用"aa".ellipsis(20);
 * 截取一个字符串,若它超过指定的长度,并在其后面增加一个省略号('...')
 * </pre>
 * @param len 允许截取的最大长度
 * @returns 返回转换后的字符串
 */
String.prototype.ellipsis=function(len){
	if(this && this.length > len){
		return this.substring(0, len-3)+"...";
	}
	return this;
};


 

 

 

前台界面代码:

<%@ page language="java" import="java.util.*" pageEncoding="GBK"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>My JSP 'index.jsp' starting page</title>
	<meta http-equiv="pragma" content="no-cache">
	<meta http-equiv="cache-control" content="no-cache">
	<meta http-equiv="expires" content="0">    
	<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
	<meta http-equiv="description" content="This is my page">
	<!--
	<link rel="stylesheet" type="text/css" href="styles.css">
	-->
	<link rel="stylesheet" type="text/css" href="./css/ext-all.css" />
	<!-- 导入写好的函数 -->
	<script type="text/javascript" src="./js/format.js"></script>
	<script type="text/javascript">
		function ddd(){
		 var aaa = "GWT-Ext does not distribute ExtJS. Note that GWT-Ext only supports";
		
		//var bbb = ellipsis(aaa,20);
		//alert(bbb);
		
		/*var ccc = ellipsis(aaa,500);
		alert(ccc);
		
		Ext.onReady(function(){
			var eee = Ext.util.Format.ellipsis(aaa,20);
			Ext.MessageBox.alert("xxx",eee);
			
		}); */
			var aaa = "GWT-Ext does not distribute ExtJS. Note that GWT-Ext only supports";
			alert(aaa.ellipsis(20));
		}
		

	</script>
  </head>
  
  <body onload="ddd();">
    This is my JSP page. <br>
    <input type="button" value="showWindow" name="showWindow" onclick="showForm(this);" title="showWindow"></input>
  </body>
</html>


 

 

运行结果跟上面的一样。

 

 

注意:

prototype的解释:

定义和用法

prototype 属性使您有能力向对象添加属性和方法。

语法

object.prototype.name=value

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

psyuhen

你的鼓励是我最大的动力谢谢支持

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值