單體創(chuàng)建對(duì)象
var Tom = {
// 屬性
name:'tom',
age:18,
// 方法
showName:function(){
alert(this.name);
},
showAge:function(){
alert(this.age);
}
}
//調(diào)用屬性
alert(Tom.name);
alert(Tom.age);
//調(diào)用方法
Tom.showName();
工廠模式創(chuàng)建對(duì)象
function Person(name,age,job){
//創(chuàng)建一個(gè)空對(duì)象
// var o = new Object();//方式一
var o = {};//方式二
o.name = name;
o.age = age;
o.job = job;
o.showName = function(){
alert(this.name);
}
o.showAge = function(){
alert(this.age);
}
o.showJob = function(){
alert(this.job);
}
return o;
}
var Tom = Person('tom',18,'程序猿');
Tom.showJob();
var Jack = Person('jack',19,'攻城獅');
Jack.showJob();
構(gòu)造函數(shù)
function Person(name,age,job){
this.name = name;
this.age = age;
this.job = job;
this.showName = function(){
alert(this.name);
}
this.showAge = function(){
alert(this.age);
}
this.showJob = function(){
alert(this.job);
}
}
//new的作用就相當(dāng)于工廠模式中最開始創(chuàng)建了一個(gè)空對(duì)象幻馁,最后把對(duì)象返回
var Bob = new Person('bob',18,'產(chǎn)品汪');
Bob.showJob();
var Alex = new Person('alex',19,'運(yùn)營喵');
Alex.showJob();
alert(Bob.showName == Alex.showName);//false
原型模式
function Person(name,age,job){
this.name = name;
this.age = age;
this.job = job;
Person.prototype.showName = function(){
alert(this.name);
}
Person.prototype.showAge = function(){
alert(this.age);
}
Person.prototype.showJob = function(){
alert(this.job);
}
}
//先去自己的對(duì)象中找showName函數(shù),再去構(gòu)造函數(shù)的原型找
var Lucy = new Person('lucy',18,'測試鼠');
//重寫自身對(duì)象中的方法越锈,不會(huì)影響其它對(duì)象
Lucy.showName = function(){
alert('我的名字是' + this.name);
}
Lucy.showName();//我的名字是lucy
var Lily = new Person('lily',19,'市場雞');
Lily.showName();//lily
alert(Lucy.showName == Lily.showName);//false
call和apply的區(qū)別
二者都可以改變當(dāng)前的this仗嗦,區(qū)別在于apply方法要將參數(shù)放入數(shù)組中再傳參
function aa(a,b){
alert('我的this是' + this + ',我的a是' + a + ',我的b是' + b);
}
//我的this是[object Window],我的a是2,我的b是3
// aa(2,3);
//我的this是abc,我的a是2,我的b是3
// aa.call('abc',2,3);
//我的this是abc,我的a是2,我的b是3
aa.apply('abc', [2,3]);
函數(shù)的繼承
//父類
function Fclass(name, age){
this.name = name;
this.age = age;
}
Fclass.prototype.showName = function(){
alert(this.name);
}
Fclass.prototype.showAge = function(){
alert(this.age);
}
//子類
function Sclass(name, age, job){
//屬性用call或者apply的方式來繼承
Fclass.call(this, name, age);
this.job = job;
}
//方法繼承:將父類的一個(gè)實(shí)例賦值給子類的原型屬性
Sclass.prototype = new Fclass();
Sclass.prototype.showJob = function(){
alert(this.job);
}
//由于已經(jīng)繼承了父類的屬性和方法,所以可以直接調(diào)用
var Driver = new Sclass('tom',18,'老司機(jī)');
Driver.showName();
Driver.showAge();
Driver.showJob();
jquery介紹
jQuery是目前使用最廣泛的javascript函數(shù)庫甘凭。據(jù)統(tǒng)計(jì)稀拐,全世界排名前100萬的網(wǎng)站,有46%使用jQuery丹弱,遠(yuǎn)遠(yuǎn)超過其他庫德撬。微軟公司甚至把jQuery作為他們的官方庫铲咨。
jQuery的版本分為1.x系列和2.x、3.x系列蜓洪,1.x系列兼容低版本的瀏覽器纤勒,2.x、3.x系列放棄支持低版本瀏覽器隆檀,目前使用最多的是1.x系列的摇天。
jquery是一個(gè)函數(shù)庫,一個(gè)js文件恐仑,頁面用script標(biāo)簽引入這個(gè)js文件就可以使用泉坐。
jQuery加載
$(function(){
var $div = $('#div');//CSS樣式怎么寫,這里就怎么寫
//html()方法相當(dāng)于原生JS的innerHTML
alert($div.html() + 'jQuery');
})