原型

構造函數

function Box(){
    this.name="box";
    this.getinfo=function(){
        console.log(this.name)
    }
}
Box.prototype.age=12;

實例化對象(只要進行了實例化操作,那麼box立馬指向Box的原型)

var box=new Box();

定義普通對象

var big={name:"ls"};

isPrototypeOf方法判斷原型是否是某個對象的原型

console.log(Box.prototype.isPrototypeOf(box))
console.log(Box.prototype.isPrototypeOf(big))

hasOwonProperty方法判斷屬性是實例的還是原型
查找順序是優先實例

console.log(Box.hasOwnProperty('age'))
console.log(Box.hasOwnProperty('name'))

原型繼承

function Box() { this.name = 'Lee';
}
function Desk() { this.age = 100;
}
Desk.prototype = new Box();
var desk = new Desk(); alert(desk.age); alert(desk.name);
function Table() { this.level = 'AAAAA';
}
Table.prototype = new Desk();
var table = new Table(); alert(table.name);

字符串反轉

//方法一
var str="abcd"
console.log(str.split("").reverse().join(""));

//方法二
var s=[];
for(var i=str.length-1;i>=0;i--){
    s.push(str[i]);
}
console.log(s.join(""));
發佈了51 篇原創文章 · 獲贊 9 · 訪問量 5萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章