天天看點

javascript進階文法第一節

一、截取的字元串的索引的位置

var str = "我是程式熱愛着";

var key = "程式";

var index = str.indexOf(key)

console.log(index);
           

二、從指定的位置開始截取字元串,截取兩個即可

var spilce = str.substr(index,2);

console.log(spilce);
           

三、大小寫的轉換

var str = "ashhhHGJLHGhhlh";

str = str.toLocaleLowerCase();

str = str.toLocaleUpperCase();
           

四、幹掉字元串兩端的空格

var str = "aa dd ggg ";

str = str.trim();

           

五、建立工廠函數—》解決了建立多個相似對象代碼備援的問題

function createPerson(name,age){

this.name = name;

this.age = age;

this.sayName = function(){

console.log("名字是:" + this.name)

}

//建立對象執行個體

var person  = createPerson("小明",20);

person.sayName ();-->輸出小明

}

           

六、 輸出對象的所有方法和屬性

//1、數組對象
        var array =new Array(11,22,33,44,55);
        array.join("|");
        console.dir(array);

        //2、日期對象
        var d = new Date();
        var day = d.getDay;
        console.dir(d);
        console.dir(day);        
        
		console.dir();可以顯示一個對象所有的屬性和方法。

		console.log();顯示在控制台的資訊
           

七、自調函數

//自調函數  函數在頁面加載時就已經調用

    (function(){
​        console.log("ok");
​    })();
           

八、執行個體對象對執行個體函數和原型屬性的搜尋

//構造函數
        function Person(name,sex,age){
            this.name = name;
            this.sex = sex;
            // this.age = age;
            this.play = function(){
                console.log("人都會有貪玩的屬性");
            }
        }

        //添加原型屬性
        Person.prototype.age = 2019;
        Person.prototype.eat = function(){
            console.log("人們都會吃飯的動作");
        }

    /*
    * 執行個體對象使用的屬性或者方法,先在執行個體中查找,找到了則直接使用
    找不到則,去執行個體對象的__proto__指向的原型對象prototype中找,找到了則使用,找不到則報錯
    *例如:age:2019
    *
    * */
        var p = new Person("小明","男");
        console.log(p.sex);
        console.dir(p.age);
        alert(p.eat);
           

九、原型中的方法是可以互相通路的

//**********************************
        function Aniaml(color) {
            this.color = color;
            this.eat = function () {
                alert("貓吃老鼠");
            };
            this.shit = function () {
                alert("貓會拉屎");
                this.eat();  //執行個體對象的方法,是可以互相調用的
            }
        }
        Aniaml.prototype.Play = function () {
            console.log("貓會跑步");
            this.Jump(); //原型對象中的方法,可以互相調用
        }
        Aniaml.prototype.Jump = function () {
            console.log("貓會跳躍");
        }
        var dog = new Aniaml("白色");
        dog.Play();
        dog.shit();
        //**************************************



        //**************************************
        //原型函數的寫法
        //原型的作用之一:資料共享、節省記憶體空間
        function Computer(price,brand){
            this.price = price;
            this.brand = brand;
        }
        Computer.prototype = {
            //手動修改構造器的指向
            constructor:Computer,
            weight:"22kg",
            configure:function(){
                console.log("該電腦的配置是i7");
            },
            color:function(){
                console.log("該電腦的顔色是白色");
            }
        }
        var C = new Computer(1200,"華碩");
        C.color();
        C.configure();
        console.dir(Computer);

        //**************************************