87,907
社区成员
发帖
与我相关
我的任务
分享
function showName(obj){
alert(obj.name);
}
// 调用
var Tom = {name:"Tom"};
showName(Tom); // Tom
<html>
<head>
<title></title>
<script type="text/javascript">
function showName(obj){
alert(obj.name);
}
window.onload = function(){
var Jim = {name:"Jim"};
var Lucy = {name:"Lucy"};
var buttons = '<input type="button" value="点击" onclick="showName(Jim)">'
+'<input type="button" value="点击" onclick="showName(Lucy)">';
document.body.innerHTML += buttons;
}
</script>
</head>
<body>
</body>
</html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
<script type="text/javascript">
<!--
var FunUtil = (function(){
var index = 0;
var handlerStore = []; // 存放方法句柄
var paramStore = []; // 存放参数
return {
// 创建方法字符串
// scope:作用域
// methodName:方法名,字符串格式
// param:参数
createFun:function(scope,methodName,param){
var currentIndex = index++; // 创建索引
// 根据索引储存方法句柄和方法参数
handlerStore[currentIndex] = function(param){
scope[methodName](param)
};
paramStore[currentIndex] = param;
return 'FunUtil._runFun('+currentIndex+')';
}
// 执行方法
// index:索引.根据这个索引找到方法句柄和方法参数
,_runFun:function(index){
var handler = handlerStore[index];
var param = paramStore[index];
handler(param);
}
};
})();
function showName(obj){
alert(obj.name);
}
window.onload = function(){
var Jim = {name:"Jim"};
var Lucy = {name:"Lucy"};
var buttons = '<input type="button" value="点击" onclick="showName(Jim)">'
+'<input type="button" value="点击" onclick="showName(Lucy)">'
// 使用工具
+'<input type="button" value="使用工具" onclick="'+FunUtil.createFun(window,'showName',Jim)+'">'
+'<input type="button" value="使用工具" onclick="'+FunUtil.createFun(window,'showName',Lucy)+'">';
document.body.innerHTML += buttons;
}
//-->
</script>
</head>
<body>
</body>
</html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
<script type="text/javascript">
<!--
/*
使用方法:
FunUtil.createFun(scope,'some_mothod_name',obj1);
FunUtil.createFun(scope,'some_mothod_name',obj1,obj2);
...可以传入多个参数
*/
var FunUtil = (function(){
var index = 0;
var handlerStore = []; // 存放方法句柄
return {
// scope:作用域
// methodName:方法名,字符串格式
// ...:参数可放多个
createFun:function(scope,methodName){
var currentIndex = index++; // 创建索引
var argu = []; // 用来存放多个参数
// 构建参数
for(var i=2,len=arguments.length;i<len;i++){
argu.push(arguments[i]);
}
// 把函数句柄存在数组里
handlerStore[currentIndex] = (function(scope,methodName,argu){
// 生成函数调用句柄
return function(){
scope[methodName].apply(scope,argu);
}
}(scope,methodName,argu));
return 'FunUtil._runFun('+currentIndex+')';
}
// 执行方法
// index:索引.根据这个索引找到执行函数
,_runFun:function(index){
var handler = handlerStore[index];
handler();// 该函数已经传入了参数
}
};
})();
function showName(obj1,obj2){
alert(obj1.name + " and " + obj2.name);
}
window.onload = function(){
var Jim = {name:"Jim"};
var Lucy = {name:"Lucy"};
var aa = {name:'aa'};
var bb = {name:'bb'};
var buttons = '<input type="button" value="点击" onclick="showName(Jim)">'
+'<input type="button" value="点击" onclick="showName(Lucy)">'
// 使用工具,传入多个参数
+'<input type="button" value="使用工具" onclick="'+FunUtil.createFun(window,'showName',Jim,Lucy)+'">'
+'<input type="button" value="使用工具" onclick="'+FunUtil.createFun(window,'showName',aa,bb)+'">' ;
document.body.innerHTML += buttons;
}
//-->
</script>
</head>
<body>
</body>
</html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
<script type="text/javascript">
var FunUtil = (function(){
var index = 0;
var handlerStore = []; // 存放方法句柄
return {
// scope:作用域
// methodName:方法名,字符串格式
// ...:参数可放多个
createFun:function(scope,methodName){
var currentIndex = index++; // 创建索引
var argu = []; // 用来存放多个参数
// 构建参数
for(var i=2,len=arguments.length;i<len;i++){
argu.push(arguments[i]);
}
// 把函数句柄存在数组里
handlerStore[currentIndex] = (function(scope,methodName,argu){
// 生成函数调用句柄
return function(){
scope[methodName].apply(scope,argu);
}
}(scope,methodName,argu));
return 'FunUtil._runFun('+currentIndex+')';
}
// 执行方法
// index:索引.根据这个索引找到执行函数
,_runFun:function(index){
var handler = handlerStore[index];
handler();// 该函数已经传入了参数
}
};
})();
var Grid = function(){
this.row = [
{id:1,name:'Jack'}
,{id:2,name:'Tom'}
,{id:3,name:'Lucy'}
];
}
Grid.prototype.updateData = function(obj){
// 修改信息,可以跳转到另一个页面或者弹窗
alert('修改:'+obj.name+',主键ID:'+obj.id);
}
Grid.prototype.sendToBody = function(){
var html = '';
for(var i in this.row){
var obj = this.row[i];
html += (obj.id + ' ' + obj.name + ' <a href="javascript:void(0)" onclick="'+FunUtil.createFun(this,'updateData',obj)+'">修改</a><br>');
}
document.body.innerHTML += html;
}
window.onload = function(){
var grid = new Grid();
grid.sendToBody();
}
</script>
</head>
<body>
</body>
</html>
window.onload = function(){
// 内部类
function Inner(){
var name = "Jim";
this.showName = function(){
alert(name);
}
}
var inner = new Inner();
// 通过<A>标签在外部作用域调用inner.showName()
document.body.innerHTML += '<a href="javascript:'+FunUtil.createFun(inner,'showName')+'">Test</a><br>';
document.body.innerHTML += '<a href="javascript:inner.showName()">Test2</a><br>'; // error
}
// 查看商品详情
function showDetail(goods){
alert('厂商:'+goods.factory+',价格:'+goods.price);
}
window.onload = function(){
// 从后台获取商品列表
var goodsItems = [
{name:'手机',factory:'NOKIA',price:1000}
,{name:'电脑',factory:'Apple',price:2000}
]
// 生成html,点击查看商品详情
for(var index in goodsItems){
var goods = goodsItems[index];
//document.body.innerHTML += '<a href="javascript:showDetail('+goods+')">'+goods.name+'</a><br>'; // error :(
document.body.innerHTML += '<a href="javascript:'+FunUtil.createFun(window,'showDetail',goods)+'">'+goods.name+'</a><br>'
}
}
当然可以传一个商品ID,通过商品ID查询商品信息再显示,这样的话就多了一步数据库操作了,显然是不好的 <script type="text/javascript">
<!--
//各函数私有变量不能互相访问,那我把他们都请到我家来不就解决问题。
var FunUtil = (function(){
return function(param,k){
if(k){
showName(arr[--k]);//直接调用函数
}else{
!this.arr&&(arr=[]);
arr.push(param);//存储变量,也是复制一下变量
return "FunUtil('',"+(arr.length)+")";
}
};
})();
function showName(obj){
alert(obj.name);
}
window.onload = function(){
var Jim = {name:"Jim"};
var Lucy = {name:"Lucy"};
var buttons = '<input type="button" value="没有工具" onclick="'+FunUtil(Jim)+'">'
+'<input type="button" value="没有工具" onclick="'+FunUtil(Lucy)+'">';
document.body.innerHTML += buttons;
};
//-->
</script>
没有那么神秘。