SPA-2~單頁(yè)應(yīng)用需要了解的相關(guān)知識(shí)點(diǎn)

SPA主要知識(shí)點(diǎn)

1.SPA模型
13.jpg

1.jQuery event function return false

通常在jQuery事件處理程序中返回false
1.告訴jQuery阻止正在操作的對(duì)象的默認(rèn)行為撮奏,像點(diǎn)擊鏈接或者選擇文字琐驴,可以在事件處理程序中調(diào)用event.preventDefault()來(lái)獲得相同的效果沐悦。
2.告訴jQuery停止該事件觸發(fā)父DOM元素上的相同事件(冒泡),可以在事件處理程序中調(diào)用event.stopPropagation()來(lái)獲得相同效果鹏氧。


2.shell

shell是單頁(yè)的主控制器墓毒,在我們的架構(gòu)中是必需的恕沫。
主要負(fù)責(zé):
1.渲染和管理功能容器
2.管理應(yīng)用狀態(tài)
3.協(xié)調(diào)功能模塊


3.布局:
33333.jpg
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>SPA chapter 1 section 1.2.2</title>
</head>
<body>
    <div id="spa">
      /*嵌入logo胆萧,賬戶(hù)設(shè)置和head容器中的搜索框*/
      <div class="spa-shell-head">
        <div class="spa-shell-head-logo"></div>
        <div class="spa-shell-head-acct"></div>
        <div class="spa-shell-head-search"></div>
      </div>
      /*將導(dǎo)航(nav)和content容器放在主容器里面*/
      <div class="spa-shell-main">
        <div class="spa-shell-main-nav"></div>
        <div class="spa-shell-main-content"></div>
      </div>
      /*創(chuàng)建footer容器*/
      <div class="spa-shell-foot"></div>
      /*將chat容器固定在外部容器的右下角*/
      <div class="spa-shell-chat"></div>
      /*創(chuàng)建modal容器,漂浮在其它內(nèi)容的上面*/
      <div class="spa-shell-modal"></div>
    </div>
</body>
</html>
    .spa-shell-head,.spa-shell-head-logo,.spa-shell-head-acct,
        .spa-shell-head-search,.spa-shell-main,.spa-shell-main-nav,
        .spa-shell-main-content,.spa-shell-foot,.spa-shell-chat,
        .spa-shell-modal{
            position: absolute;
        }
        .spa-shell-head{
            top: 0;
            left: 0;
            right: 0;
            height: 40px;
        }
        .spa-shell-head-logo{
            top: 4px;
            left: 4px;
            height: 32px;
            width: 128px;
            background-color: orange;
        }
        .spa-shell-head-acct{
            top:4px;
            right: 0;
            width: 64px;
            height: 32px;
            background-color: green;
        }
        .spa-shell-head-search{
            top: 4px;
            right: 64px;
            width: 248px;
            height: 32px;
            background:blue;
        }
        .spa-shell-main{
            top:40px;
            left: 0;
            bottom: 40px;
            right: 0;
        }
        .spa-shell-main-content,
        .spa-shell-main-nav{
            top:0;
            bottom: 0;
        }
        .spa-shell-main-nav{
            width: 250px;
            background:#eee;
        }
        .spa-x-closed .spa-shell-main-nav{
            width: 0;
        }
        .spa-shell-main-content{
            left: 250px;
            right: 0;
            background:#ddd;
        }
        .spa-shell-closed .spa-shell-main-content{
            left:0;
        }
        .spa-shell-foot{
            bottom: 0;
            right: 0;
            left: 0;
            height: 40px;
        }
        .spa-shell-chat{
            bottom: 0;
            right: 0;
            width: 300px;
            height: 15px;
            cursor:pointer;
            background: red;
            z-index: 1;
            border-radius: 5px 0 0 0;
        }
            .spa-shell-chat:hover{
                background-color: #a00;
            }
        .spa-shell-modal{
            margin-top: -200px;
            margin-left: -200px;
            top: 50%;
            left: 50%;
            width: 400px;
            height: 400px;
            background:#fff;
            border-radius:3px;
            z-index: 2;
        }

所有選擇器的前綴都是spa-shell- 這樣有很多好處
1.表明這些類(lèi)都是由Shell模塊(spa/js/spa.shell.js)控制的
2.這能防止和第三方腳本以及我們的其它模塊產(chǎn)生名字空間的沖突
3.在調(diào)試和查看HTML文檔的時(shí)候膳叨,我們能立即明白哪些元素是由Shell模塊生成和控制的洽洁。


4.URI

之后會(huì)通過(guò)這個(gè)屬性進(jìn)行應(yīng)用的狀態(tài)管理。


5.jquery.urianchor

uri操作插件,以及相關(guān)驗(yàn)證的使用說(shuō)明


6.hasOwnProperty

獲取object非prototype的屬性


7.
KEYVAL:for(...){
...
}
//http://stackoverflow.com/questions/30154212/what-is-keyval-and-what-its-intended-for

主要是for循環(huán)的一個(gè)標(biāo)識(shí)菲嘴,可以配合continue和break操作相應(yīng)的for循環(huán)


8.delete

The delete operator removes a property from an object.


9.自執(zhí)行匿名函數(shù)-Immediately-invoked function expression
advantage

An immediately-invoked function expression (or IIFE, pronounced "iffy"[1]) is a JavaScript design pattern which produces a lexical scope using JavaScript's function scoping. Immediately-invoked function expressions can be used to avoid variable hoisting from within blocks, protect against polluting the global environment and simultaneously allow public access to methods while retaining privacy for variables defined within the function. This pattern has been referred to as a self-executing anonymous function,[2] but Ben Alman introduced the term IIFE as a more semantically accurate term for the pattern, shortly after its discussion arose on comp.lang.javascript.
---即保持變量的私有诡挂,防止污染全局環(huán)境碎浇。

usage

Immediately-invoked function expressions may be written in a number of different ways. A common convention is to enclose the function expression (and optionally its invocation operator—Douglas Crockford's style) in parentheses to explicitly tell the parser to expect an expression, since in JavaScript parentheses can't contain statements. Otherwise, in most situations, when the parser encounters the function keyword, it treats it as a function declaration (statement), and not as a function expression.
---自執(zhí)行匿名函數(shù)其實(shí)是一個(gè)表達(dá)式

(function () { … })();
(function () { … }());
!function () { … }();
~function () { … }();
-function () { … }();
+function () { … }();
//In contexts where an expression is expected, wrapping in parentheses is not necessary:
var f = function () { … }();
true && function () { … }();
0, function () { … }();

10.Object.create()

實(shí)例化對(duì)象,一般使用new操作符璃俗,但是這容易被誤認(rèn)為基于類(lèi)的開(kāi)發(fā)。而用Object.create更符合基于原型開(kāi)發(fā)的感覺(jué)悉默。

//工廠(chǎng)模式和Object.create()實(shí)例化對(duì)象
var proto={
  sentence:4,
  probation:2
};
var makePrisoner=function(name,id){
   var prisoner=Object.create(proto);
  prisoner.name=name;
  prisoner.id=id;
  return prisoner;
};
var firstPrisoner=makePrisoner('Joe','12A');
var secondPrisoner=makePrisoner('Sam','2BC');

假設(shè)是ie6城豁,7,8

//Cross-browser method to support Object.create()
var objectCreate=function(arg){
  if(!arg){return {};}
  function obj(){};
  obj.prototype=arg;
  return new obj;
}

11.模塊模式為什么使用閉包
var prison=(function(){
    var prisoner_name='Mike Mikowski',
            jail_term='20 year term';

            return {
                prisoner:prisoner_name+'-'+jail_term,
                sentence:jail_term
            };
})();

//this is undefined ,no prisoner_name for you
console.log(prison.prisoner_name);

//this outputs 'Mike Mikowski -20 year term'
console.log(prison.prisoner);

//這里有一個(gè)問(wèn)題抄课,一旦自執(zhí)行匿名函數(shù)停止執(zhí)行唱星,在它里面定義的變量都沒(méi)有了,所以它們是不能更改的
// 即
prison.jail_term='Sentence commuted';
//this still outputs 'Mike Mikowski -20 year term'
console.log(prison.prisoner);

// 但是我們可以把屬性變成方法跟磨,即通過(guò)閉包來(lái)保存和設(shè)置變量,把變量的值保存在內(nèi)存中
var prison=(function(){
    var prisoner_name='Mike Mikowski',
        jail_term='20 year term';

        return {
            prisoner:function(){
                return prisoner_name + '-'+jail_term;
            },
            setJailTerm:function(){
                jail_term=term;
            }
        }
})()
//this outputs 'Mike Mikowski -20 year term'
console.log(prison.prisoner);

prison.setJailTerm('Sentence commuted');

//this now outputs 'Mike Mikowski - Sentence commuted'
console.log(prison.prisoner());

12.閉包c(diǎn)losure

一般函數(shù)執(zhí)行完畢就會(huì)釋放內(nèi)存间聊,閉包就是阻止垃圾回收器將變量從內(nèi)存中移除的方法,使得在創(chuàng)建變量的執(zhí)行環(huán)境的外面能夠訪(fǎng)問(wèn)到該變量抵拘。閉包最主要的是保存函數(shù)的執(zhí)行環(huán)境.函數(shù)每次調(diào)用都會(huì)創(chuàng)建一個(gè)唯一的執(zhí)行環(huán)境哎榴,函數(shù)執(zhí)行完后,執(zhí)行環(huán)境就會(huì)被丟棄僵蛛,除非有調(diào)用者引用了它(閉包)

Closures are functions that refer to independent (free) variables (variables that are used locally, but defined in an enclosing scope). In other words, these functions 'remember' the environment in which they were created.

var makePrison=function(prisoner){
    return function(){
        return prisoner;
    }
};
var joshPrison=makePrison('Josh Powell');
var mikePrison=makePrison('Mike Mikowski');

console.log(joshPrison);//outputs Josh Powell
console.log(mikePrison);//outputs Mike Mikowski
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末尚蝌,一起剝皮案震驚了整個(gè)濱河市,隨后出現(xiàn)的幾起案子充尉,更是在濱河造成了極大的恐慌飘言,老刑警劉巖,帶你破解...
    沈念sama閱讀 217,406評(píng)論 6 503
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件驼侠,死亡現(xiàn)場(chǎng)離奇詭異姿鸿,居然都是意外死亡,警方通過(guò)查閱死者的電腦和手機(jī)倒源,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,732評(píng)論 3 393
  • 文/潘曉璐 我一進(jìn)店門(mén)苛预,熙熙樓的掌柜王于貴愁眉苦臉地迎上來(lái),“玉大人相速,你說(shuō)我怎么就攤上這事碟渺。” “怎么了突诬?”我有些...
    開(kāi)封第一講書(shū)人閱讀 163,711評(píng)論 0 353
  • 文/不壞的土叔 我叫張陵苫拍,是天一觀的道長(zhǎng)。 經(jīng)常有香客問(wèn)我旺隙,道長(zhǎng)绒极,這世上最難降的妖魔是什么? 我笑而不...
    開(kāi)封第一講書(shū)人閱讀 58,380評(píng)論 1 293
  • 正文 為了忘掉前任蔬捷,我火速辦了婚禮垄提,結(jié)果婚禮上榔袋,老公的妹妹穿的比我還像新娘。我一直安慰自己铡俐,他們只是感情好凰兑,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,432評(píng)論 6 392
  • 文/花漫 我一把揭開(kāi)白布。 她就那樣靜靜地躺著审丘,像睡著了一般吏够。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上滩报,一...
    開(kāi)封第一講書(shū)人閱讀 51,301評(píng)論 1 301
  • 那天锅知,我揣著相機(jī)與錄音,去河邊找鬼脓钾。 笑死售睹,一個(gè)胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的可训。 我是一名探鬼主播昌妹,決...
    沈念sama閱讀 40,145評(píng)論 3 418
  • 文/蒼蘭香墨 我猛地睜開(kāi)眼,長(zhǎng)吁一口氣:“原來(lái)是場(chǎng)噩夢(mèng)啊……” “哼沉噩!你這毒婦竟也來(lái)了捺宗?” 一聲冷哼從身側(cè)響起,我...
    開(kāi)封第一講書(shū)人閱讀 39,008評(píng)論 0 276
  • 序言:老撾萬(wàn)榮一對(duì)情侶失蹤川蒙,失蹤者是張志新(化名)和其女友劉穎蚜厉,沒(méi)想到半個(gè)月后,有當(dāng)?shù)厝嗽跇?shù)林里發(fā)現(xiàn)了一具尸體畜眨,經(jīng)...
    沈念sama閱讀 45,443評(píng)論 1 314
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡昼牛,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,649評(píng)論 3 334
  • 正文 我和宋清朗相戀三年,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了康聂。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片贰健。...
    茶點(diǎn)故事閱讀 39,795評(píng)論 1 347
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡,死狀恐怖恬汁,靈堂內(nèi)的尸體忽然破棺而出伶椿,到底是詐尸還是另有隱情,我是刑警寧澤氓侧,帶...
    沈念sama閱讀 35,501評(píng)論 5 345
  • 正文 年R本政府宣布脊另,位于F島的核電站,受9級(jí)特大地震影響约巷,放射性物質(zhì)發(fā)生泄漏偎痛。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,119評(píng)論 3 328
  • 文/蒙蒙 一独郎、第九天 我趴在偏房一處隱蔽的房頂上張望踩麦。 院中可真熱鬧枚赡,春花似錦、人聲如沸谓谦。這莊子的主人今日做“春日...
    開(kāi)封第一講書(shū)人閱讀 31,731評(píng)論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)反粥。三九已至料皇,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間星压,已是汗流浹背。 一陣腳步聲響...
    開(kāi)封第一講書(shū)人閱讀 32,865評(píng)論 1 269
  • 我被黑心中介騙來(lái)泰國(guó)打工鬼譬, 沒(méi)想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留娜膘,地道東北人。 一個(gè)月前我還...
    沈念sama閱讀 47,899評(píng)論 2 370
  • 正文 我出身青樓优质,卻偏偏與公主長(zhǎng)得像竣贪,于是被迫代替她去往敵國(guó)和親。 傳聞我的和親對(duì)象是個(gè)殘疾皇子巩螃,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,724評(píng)論 2 354

推薦閱讀更多精彩內(nèi)容

  • **2014真題Directions:Read the following text. Choose the be...
    又是夜半驚坐起閱讀 9,495評(píng)論 0 23
  • 東家自己叫酷魚(yú)演怎,管這只狗東西叫做了雪茄。說(shuō)是一聲叫喚避乏,便有了兩種念想爷耀。 酷魚(yú)開(kāi)著間四驅(qū)改裝車(chē)行,修葺一新拍皮,我說(shuō)來(lái)坐...
    維耕思讀閱讀 167評(píng)論 0 0
  • 關(guān)于敘事歹叮、隱喻和自由寫(xiě)作 這本書(shū)從去年底開(kāi)始一直在斷斷續(xù)續(xù)地讀,沒(méi)想到今天早晨用差不多三個(gè)小時(shí)的時(shí)間就讀完了铆帽,也毫...
    wulipaboo閱讀 1,542評(píng)論 0 2