以下為本站文章分類清單

  1. AJAXspacer
  2. ASPspacer
  3. CSSspacer
  4. Javascriptspacer
  5. Windows 應用程式spacer
  6. 網頁設計
  7. 評論、短文、雜文spacer
  8. 版主spacer
  9. 最新回應spacer

以下為本頁本文內容

由 Closure 這個字談起

3/25 12' icon

Photo by About.com

Closure 在字典上的意思有:

我們注意到了最後一項應用於數理方面的說明:閉包。很怪的中文,不過在網頁中,很早就出現,也就是 HTML 標籤中的 </>,部份 HTML 標籤需要有成對的"結束標籤",例如 <p>...</p>。這叫作 Closing Tag 或 Tag closure。

雖然大多數情況下,該寫 Closing Tag 的標籤,不寫也能"正常"顯示 (拜 M$ IE 4/5/6.....? 之賜,給設計師無限的偷懶空間及方便),不過,愈來愈進化的各式網頁工具,對語法結構的要求會愈來愈嚴格,如果不是太麻煩,建議你該 Closing 的就 Closing。

而在所謂現代 Javascript 陳述式中,也加入了 Closure (歸屬於 Javascript 1.8),稱之為 Expression Closure (閉包陳述? 太怪了,還是講原文好了),看個例子:

// 傳統寫法
function sum(x){
	return (1+x)*x/2;
}
document.write (sum(10));

// Expression Closure
document.write ((function(x)(1+x)*x/2)(10));

看懂嗎,整個變化是這樣子的:

function sum(x){return (1+x)*x/2;}
sum(10);  //變化成以下

var sum=function(x){return (1+x)*x/2;}
sum(10);  //變化成以下

// 把第 4 行的 sum = 後方的所有內容,置換第 5 行的 sum
function(x){return (1+x)*x/2;}(10);

// (10) 前的所有陳述是一體的,就像先加減後乖除一樣,如果必需強調要先處理,則必需用 () 抱含
(function(x){return (1+x)*x/2;})(10);

// 因為 function(x) 直接透過一行運算就得到結果,並回傳,所以,連 {} 及 return 都省了
(function(x)(1+x)*x/2)(10);

看起來很面熟嗎?這就是 Javascript 1.8 導入的 Expression Closure,至於為什麼要這麼作呢? MDC (Mozilla 研發中心) 官網上是這麼說的

This addition is nothing more than a shorthand for writing simple functions, giving the language something similar to a typical Lambda notation

除了縮短語法,暫時還看不出有什麼好處。最後提醒,這屬於現代瀏覽器語法,目前不是所有瀏覽器都支援這種語法結構

 

以下為文章回應區

同意轉載,不過麻煩看一下轉載需知

Can you have an operation to make you taller?   2017/7/15 下午 12:20:00

That is very attention-grabbing, You are an overly professional blogger.
I have joined your rss feed and look ahead to seeking extra of your fantastic post.
Additionally, I have shared your web site in my social networks

ed sheeran videos   2017/7/8 下午 06:10:00

magnificent points altogether, you simply received a emblem new reader.
What could you suggest in regards to your put up that you simply made a
few days ago? Any sure?

foot pain icd 10   2017/7/4 上午 06:33:00

My brother suggested I may like this website. He
was once entirely right. This post truly made my day. You cann't consider simply how a lot time
I had spent for this info! Thank you!

   2017/6/24 下午 08:17:00

Precisely what I was looking for, regards for putting up.

doctor   2017/6/6 上午 05:38:00

I absolutely love your blog.. Excellent colors & theme.
Did you create this web site yourself? Please reply back as I'm trying to create my own personal site and would like to learn where
you got this from or what the theme is named. Thank you!

jaymiesultani.hatenablog.com   2017/5/7 下午 11:58:00

First of all I want to say wonderful blog! I had a quick question in which I'd like to ask if you don't
mind. I was curious to find out how you center yourself and clear your head prior to writing.
I've had trouble clearing my mind in getting my ideas out there.
I do take pleasure in writing but it just seems like the first 10 to 15 minutes are lost just trying to figure
out how to begin. Any ideas or hints? Thank you!

manicure   2017/5/4 上午 05:16:00

Wow, that's what I was exploring for, what a material!
present here at this website, thanks admin of this site.

manicure   2017/5/3 下午 08:37:00

Do you have any video of that? I'd like to find out more details.

manicure   2017/5/3 下午 08:13:00

Highly descriptive post, I liked that bit. Will there be a
part 2?

manicure   2017/5/3 下午 02:03:00

Hi there, yes this paragraph is truly nice and I have learned lot of things from
it regarding blogging. thanks.

manicure   2017/4/29 下午 11:20:00

I've been exploring for a bit for any high quality articles or weblog
posts in this kind of space . Exploring in Yahoo I finally stumbled upon this web site.
Reading this information So i am happy to convey that I have a very good uncanny feeling I came upon exactly what I needed.
I such a lot without a doubt will make sure to do not disregard this
website and give it a glance on a continuing basis.

manicure   2017/4/29 下午 10:55:00

Howdy! I could have sworn I've been to this blog
before but after checking through some of the post I realized it's
new to me. Anyhow, I'm definitely happy I found it and I'll be book-marking
and checking back often!

download music from soundcloud   2017/4/13 下午 11:45:00

anyone knows the name of Music 3:50

BHW   2017/4/12 下午 05:52:00

It is truly a great and helpful piece of information. I
am satisfied that you just shared this useful information with us.

Please keep us informed like this. Thank you for sharing.

給個回應
姓名:
佈落格網址:
  如果您是要發問問題, 最好有個問題測試的網址, 這樣比較容易找到您問題所在, 謝謝
內容: