1. 程式人生 > >第11天:原型與原型鏈

第11天:原型與原型鏈

1.例項物件的原型_proto_與原型物件prototype 通過_proto_聯絡起來。建構函式裡面的_proto_ 指向Object 的原型物件,Object 的原型物件的_proto_指向null. 原型物件prototype 存在於建構函式中。物件中的_proto_總是指向該物件所在(指向)的建構函式中的prototype原型物件。

2.根據例項物件找到其屬性。

若例項物件無該屬性,順應繼承鏈,往下找。

3.函式的宣告與表示式。

在函式的宣告中,有的瀏覽器會在預編譯的時候將函式宣告內容提前,在if-else 語句中,IE8瀏覽器會得到不同的結果,因此最好是採用函式的表示式。

var ff=function(){

};

4.函式都可看成是Function的建構函式例項化的 結果。var f1=new Function("num1","num2","return num1+num2");

f1(10,20);======>30;    f1._proto_==Function.prototype;  =====>true;

5.函式也是一種資料型別

6.正則表示式

.  表達除 \n 以外的任何字元;{a,b} 表達字元出現 a 到 b 次。{a,} a 到無窮次。

[ ] 表示範圍   字元組,任意的單個字元。[4]  表示4   [1-7] 表示數字1到7;

[a-zA-Z] 字元為大小寫字母。 * 表示出現0~多次。 + 表示出現1~多次。?表示出現 0~1 次。

^表示以後面字元開頭  或取反  ^[0-9] 表示以數字開頭。

$ 表示以前面的字元結尾           [a-z][0-9]$      表示以數字結尾        324a9 

[0-9a-z] 字元為0-9 的數字或小寫字母。 \d 也表示數字。\D 表示非數字===》等價於[^0-9]。

鍵盤上按鍵:數字、字母、特殊符號(_ 不算特殊符號)

特殊符號[^0-9a-zA-Z_]

限定符:

{5-10} 字元出現次數5到10次;

\s 空白符 。 \S 非空白符。

\w 表示 非特殊符號。f 等。[A-Za-z0-9_]

\W 表示特殊符號。%~等    [^A-Za-z0-9_]

郵箱正則:

eg: [email protected]

[a-zA-Z0-9_.-]+[@][0-9a-zA-Z_.-]+  ([.][a-zA-Z]+){1,2}

 

建構函式的方法建立正則表示式物件。

js 中 中間內容用// 分隔開。或者將匹配的內容放在“ ”中(在java 中或其他語言中常用),但注意的是 轉義字元 \d 

var r=new RegExp(/\d{5}/);

 var str="要我幫幫你12580";

 

var rr=new RegExp("\\d{5}");
 console.log(rr.test(str));            ==========>true;

  console.log(r.test(str));            ===========>true;

字面量方式建立:

var q=/\d+/;
 var re=q.test("kjini0");
console.log(re);                       ======>true;

7.apply ,call,bind函式

eg: 在子建構函式中呼叫父建構函式 。

function Student(name,age){

  Person.call(this,name,age);

}

apply ,call函式的作用是為了 將函式或方法 賦給當前某一物件使用,改變this 的指向。

APPLY: 函式或方法.apply(物件,[引數1,引數2]);

CALL:  函式或方法.call(物件,引數1,引數2);

bind 函式    複製函式或方法給物件  可有返回值。為複製後的函式或方法

BIND: 函式或方法.bind(物件,引數); 

eg: per.sayHi.apply(stu,[10,20]);

<script>
            function Random(){
                this.num=parseInt(Math.random()*10+1);
            }
            Random.prototype.printNum=function(){
                console.log(this.num);
            };
            
            Random.prototype.ontime=function(){
                //將this 即Random 物件傳入bind ,若為null,則表示對應函式內為window 物件
//                window.setInterval(this.printNum.bind(this),1000);

            };
            var r=new Random();
//            r.ontime();
        </script>

result: 2  2   2  2  。。。(每隔一秒輸出)

window.setInterval(function(){
                    console.log(this.num);
                }.bind(this),1000);       

result: 2  2   2  2  。。。(每隔一秒輸出)

Random.prototype.ontime=function(){
                //將this 即Random 物件傳入bind ,若為null,則表示對應函式內為window 物件
//                window.setInterval(this.printNum.bind(this),1000);
                window.setInterval(function(){
                    console.log(parseInt(Math.random()*10+1));
                }.bind(this),1000);
            };

result: 10 2 4 6   7 。。。(隨機產生數字)

注意: this.printNum() 表示呼叫函式之後的返回值。此函式無返回值。函式是命名函式作為引數時,只需傳入命名函式的名字即可,無括號。

函式作為引數,定時完成部分功能。

<script>
            function f1(fn){
                window.setInterval(function(){
                    console.log("定時開始");
                    fn();
                    console.log("定時結束");
                },1000);
            }
            function fn(){
                console.log("活到老,學到老");
            }
            f1(fn);
        </script>

[Web瀏覽器] "定時開始"    /frontEnd/bind/setInterval.html (11)

[Web瀏覽器] "活到老,學到老"    /frontEnd/bind/setInterval.html (17)
[Web瀏覽器] "定時結束"    /frontEnd/bind/setInterval.html (13)
[Web瀏覽器] "定時開始"    /frontEnd/bind/setInterval.html (11)
[Web瀏覽器] "活到老,學到老"    /frontEnd/bind/setInterval.html (17)
[Web瀏覽器] "定時結束"    /frontEnd/bind/setInterval.html (13)

 

8.函式的組成。

f1.caller();   表示呼叫f1 的物件。

f1.arguments.length 表示實參的個數。

f1.length:表示形參的個數。

f1.name: 表示函式名