javascript之内嵌函数javascript

#1楼 2008-01-30 08:27 天生俪姿

我看了你的文章感觉很不错。

不过我想问一下两个别的问题,关于页面的

第一个 你的那个“测试一下” 鼠标移上去 的显示出来的(我认为是div)测试的那个效果 是怎么做的。

第二个 不知道你是怎么写的这篇文章 为什么点击 测试一下的时候会有代码执行。是博客园就有这个功能吗? 本人可以往页面中加入代码(脚本)。

多谢。

回复 引用 查看

#2楼[楼主] 2008-01-30 08:33 agan@cn

@天生俪姿

第一个问题你可以看看我的>

http://www.cnblogs.com/agancn/archive/2008/01/26/1053875.html

第二个问题你说的没有错,你可以在文章里面添加javascript脚本,就能实现互动啦

谢谢

回复 引用 查看

#3楼 2008-01-30 09:10 在线代理[未注册用户]

拿个浏览器跑一下就知道结果了。

回复 引用

#4楼[楼主] 2008-01-30 09:18 agan@cn

@itares

我后面有解释原理,不知道你是不是看完,还是我没有解释清楚,愿听高见

回复 引用 查看

#5楼[楼主] 2008-01-30 09:19 agan@cn

@在线代理

很有才啊

回复 引用 查看

#6楼 2008-01-30 10:26 囧[未注册用户]

囧 我js太烂了 不介意的话!想请解释下

var lastret = outerret(parseint(innum));

//这里 outerret 是上面定义的变量, 那么 outerret(parm) 是什么意思 这个是调用 outerfn(parm) ??

回复 引用

#7楼 2008-01-30 10:40 杨正祎

这个文章的排版好呀。

网上的文章就要体现出和纸质的不同和优势。

要不别人老是说——还不如买本书看呢!

回复 引用 查看

#8楼 2008-01-30 10:52 cat chen

其实就是closure,理解closure就什么都好了。

回复 引用 查看

#9楼 2008-01-30 10:58 囧[未注册用户]

自己跑了下 知道是什么原因了!!谢谢楼主 ~——~

回复 引用

#10楼 2008-01-30 13:27 flymouse

能说一下那个参数怎么传递的吗?内嵌函数的secnum是怎么传递的?

回复 引用 查看

#11楼[楼主] 2008-01-30 14:10 agan@cn

@cat chen

严重赞同,不过有些难理解

回复 引用 查看

#12楼 2008-01-30 14:40 留恋星空

了解下

回复 引用 查看

#13楼 2008-01-30 15:20 闭包[未注册用户]

闭包

回复 引用

#14楼 2008-01-30 20:22 殇

还真不了解

回复 引用 查看

#15楼 2008-01-30 21:02 任力

问一下楼主,你外部方法的20为什么保存了?能否解释一下。

回复 引用 查看

#16楼[楼主] 2008-01-30 22:01 agan@cn

@闭包

英文是closure,可能翻译过来就是闭包吧

回复 引用 查看

#17楼 2008-01-30 22:16 蓝琳[未注册用户]

楼主写得不错!!!

回复 引用

#18楼[楼主] 2008-01-30 23:31 agan@cn

@任力

javascript的闭包机制

回复 引用 查看

#19楼 2008-01-31 08:43 alpha(奥法)

叫闭包更好些。

回复 引用 查看

#20楼 2008-01-31 09:23 天生俪姿

我又看了一下文章

有件事还是不理解。

你编辑这篇文章的时候用的是哪种编辑器。为什么他没有将你的javascript代码进行html编码。反而原样的存起来了,然后就能在页面中执行。我记得一般的编辑器都是要编码的。

第二个 对于这个闭包的意思,不太理解

还有一个 就是对于这个 内嵌函数的使用。因为编过的javascript代码实在是少。所以什么时候会使用到 不清楚。能不能具体的说说。

多谢楼主。。。

回复 引用 查看

#21楼 2008-01-31 09:46 flyingchen

这样写性能高吗?

回复 引用 查看

#22楼 2008-01-31 11:50 netmotor

调用outerfn时返回的是一个函数对象(就是innerfn),在该返回的函数对象后面加(),就表示执行该函数,由于该函数是在outerfn内部定义的 ,所以它可以访问outerfn内部的私有变量firnum,firnum在调用outerfn时候已经运算好了 。不知道说的对不对。

回复 引用 查看

#23楼[楼主] 2008-01-31 12:18 agan@cn

@netmotor

对的.

回复 引用 查看

#24楼[楼主] 2008-01-31 13:36 agan@cn

@flyingchen

性能没有研究过,但有些时候需要这样用.比如我们在使用定时器的时候settimeout(code,millsecnds),需要在案1秒过后,调用一个函数,如何给这个函数传参数呢,这个时候就要用到闭包也及内嵌函数.可以试试下面例子:

function test(){

var param = 'hello';

settimeout(function(){alert(param)},1000);//匿名内嵌函数

}

test();

上面代码在调用test()之后1秒种调用内嵌匿名函数,虽然test()已经返回,但是他的内嵌函数还是可以访问param变量.

回复 引用 查看

#25楼 2008-01-31 14:15 路过[未注册用户]

想正真学会js,一定要看“javascript: the definitive guide, 5th edition“,现在不知道有没有更新的中文版,把这本书k过了就ok了。

回复 引用

#26楼[楼主] 2008-01-31 14:20 agan@cn

@天生俪姿

你可以在编辑你的文章的时候选择html编辑模式,就可以写自己的脚本啦

绑定事件,封装相关的功能等等,参考例子:

/* a general function that associates an object instance with an event

handler. the returned inner function is used as the event handler.

the object instance is passed as the - obj - parameter and the name

of the method that is to be called on that object is passed as the -

methodname - (string) parameter.

*/

function associateobjwithevent(obj, methodname){

/* the returned inner function is intended to act as an event

handler for a dom element:-

*/

return (function(e){

/* the event object that will have been parsed as the - e -

parameter on dom standard browsers is normalised to the ie

event object if it has not been passed as an argument to the

event handling inner function:-

*/

e = e||window.event;

/* the event handler calls a method of the object - obj - with

the name held in the string - methodname - passing the now

normalised event object and a reference to the element to

which the event handler has been assigned using the - this -

(which works because the inner function is executed as a

method of that element because it has been assigned as an

event handler):-

*/

return obj[methodname](e, this);

});

}

/* this constructor function creates objects that associates themselves

with dom elements whose ids are passed to the constructor as a

string. the object instances want to arrange than when the

corresponding element triggers onclick, onmouseover and onmouseout

events corresponding methods are called on their object instance.

*/

function dhtmlobject(elementid){

/* a function is called that retrieves a reference to the dom

element (or null if it cannot be found) with the id of the

required element passed as its argument. the returned value

is assigned to the local variable - el -:-

*/

var el = getelementwithid(elementid);

/* the value of - el - is internally type-converted to boolean for

the - if - statement so that if it refers to an object the

result will be true, and if it is null the result false. so that

the following block is only executed if the - el - variable

refers to a dom element:-

*/

if(el){

/* to assign a function as the element's event handler this

object calls the - associateobjwithevent - function

specifying itself (with the - this - keyword) as the object

on which a method is to be called and providing the name of

the method that is to be called. the - associateobjwithevent

- function will return a reference to an inner function that

is assigned to the event handler of the dom element. that

inner function will call the required method on the

javascript object when it is executed in response to

events:-

*/

el.onclick = associateobjwithevent(this, "doonclick");

el.onmouseover = associateobjwithevent(this, "domouseover");

el.onmouseout = associateobjwithevent(this, "domouseout");

...

}

}

dhtmlobject.prototype.doonclick = function(event, element){

... // doonclick method body.

}

dhtmlobject.prototype.domouseover = function(event, element){

... // domouseover method body.

}

dhtmlobject.prototype.domouseout = function(event, element){

... // domouseout method body.

}

回复 引用 查看

#27楼[楼主] 2008-01-31 14:34 agan@cn

@路过

这本也不错

learning javascript by shelley powers

回复 引用 查看

注册用户登录后才能发表评论,请 登录 或 注册,返回博客园首页

首页博问闪存新闻园子招聘知识库

最新it新闻:

·windows 7官方rss动态主题:《昆虫》

·捡到iphone 4s玩自拍 icloud同步酿悲剧

·铁道部购票网站存泄密危险 cdn服务商技术短板是主因

·利用 mimo magictouch 打造另类的平板电脑

·但愿大嘴巴的消息再次失准

» 更多新闻...

最新知识库文章:

·设计师的品牌意识

·如何成为“10倍效率”开发者

·快速排序(quicksort)的javascript实现

·wcf服务端运行时架构体系详解[续篇]

·wcf服务端运行时架构体系详解[下篇]

» 更多知识库文章...

china-pub 2011秋季教材巡展

china-pub 计算机绝版图书按需印刷服务

copyright ©2012 agan@cn powered by: 博客园 模板提供:沪江博客


======================================================
在最后,我邀请大家参加新浪APP,就是新浪免费送大家的一个空间,支持PHP+MySql,免费二级域名,免费域名绑定 这个是我邀请的地址,您通过这个链接注册即为我的好友,并获赠云豆500个,价值5元哦!
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值