js面向?qū)ο笾?、私有、靜態(tài)屬性和方法詳解
現(xiàn)下,javascript大行其道,對于網(wǎng)站開發(fā)人員來說,javascript是必需掌據(jù)的一門語言,但隨著jquery等框架的流行和使用,許多人對于原生javascript缺乏深入的理解,習(xí)慣了函數(shù)式的編輯風(fēng)格,對于閉包、原型總是說不清道不明.對于js面向?qū)ο篚磕_的用著,而要了解js面向?qū)ο?,就必需先了解js中什么是公有方法、特權(quán)方法、靜態(tài)方法
方法/步驟
1.公有屬性和公有方法
function User(name,age){ this.name = name;//公有屬性 this.age = age; } User.prototype.getName = function(){//公有方法 return this.name; } var user = new User('fire子海',26); console.log(user.getName());//output:fire子海
2.私有屬性和方法
function User(name,age){ var name = name;//私有屬性 var age = age; function alertAge(){//私有方法 alert(age); } alertAge(age); //彈出26 } var user = new User('fire子海',26);
3.靜態(tài)屬性和方法
在php中,無需實(shí)例化就可以調(diào)用的方法就叫靜態(tài)方法,js也一樣,無需實(shí)例化,即用new操作符實(shí)化對象,就可調(diào)用對象的方法和屬性。
function User(){} User.age = 26;//靜態(tài)屬性 User.myname = 'fire子海'; User.getName =function(){//靜態(tài)方法 return this.myname;//如果這里使用this.name,返回的將是User,所有改用了myname, } console.log(User.getName());//output:fire子海
4.特權(quán)方法
function User(name,age){ var name = name;//私有屬性 var age = age; this.getName = function(){ //特權(quán)方法 return name;//私有屬性和方法不能使用this調(diào)用 } } var user = new User('fire子海',26); console.log(user.getName());//output:fire子海
5.靜態(tài)類
對于靜態(tài)方法和靜態(tài)屬性,我們無需像第三步中那樣去創(chuàng)建,如果網(wǎng)友看過我那篇“js如何制作圖片輪播”,就知道可以使用字面量的方式來創(chuàng)建。
var user = { init:function(name,age){ this.name = name; this.age = age; }, getName:function(){ return this.name; } } user.init('fire子海',26); console.log(user.getName());//output:fire子海
6.公有方法的調(diào)用規(guī)則
調(diào)用公有方法,我們必需先實(shí)例化對象
公有方法中通過不this調(diào)用公有屬性和特權(quán)方法,不能使用this調(diào)用靜態(tài)方法和屬性,必需裁通過對象本身調(diào)用,即對象名。公有方法也不能調(diào)用私有方法
function User(){ this.myname = 'fire子海';//公有屬性 this.age = 26; this.do = function(){//特權(quán)方法 return this.myname+'學(xué)習(xí)js'; } } User.eat = function(food){ return '晚餐只有'+food; } User.prototype.alertAge = function(){ alert(this.age); } User.prototype.alertDo = function(){ alert(this.do());//調(diào)用特權(quán)方法 } User.prototype.alertEat = function(food){ alert(User.eat(food));//只能通過對象本身調(diào)用靜態(tài)方法 //alert(this.ear(food))這樣調(diào)用將出錯(cuò):this.eat is not a function } var user = new User(); user.alertAge();//alert:26 user.alertDo();//alert:fire子海學(xué)習(xí)js user.alertEat('方便面')//alert:晚餐只有方便面
7.靜態(tài)方法的調(diào)用規(guī)則
使用靜態(tài)方法時(shí),無需實(shí)例化對象,便可以調(diào)用,對象實(shí)例不能調(diào)用對象的靜態(tài)方法,只能調(diào)用實(shí)例自身的靜態(tài)屬性和方法
function User(){} User.age = 26;//靜態(tài)屬性 User.myname = 'fire子海'; User.getName =function(){//靜態(tài)方法 return this.myname; } var user = new User(); console.log(user.getName);//TypeError: user.getName is not a function user.supper = '方便面'; user.eat = function(){ return '晚餐只有'+this.supper; } user.eat();//晚餐只有方便面
靜態(tài)方法無法調(diào)用公有屬性、公有方法、私有方法、私有屬性、特權(quán)方法和原型屬性
function User(){ this.myname = 'fire子海';//公有屬性 this.age = 26; this.do = function(){//特權(quán)方法 return this.myname+'學(xué)習(xí)js'; } } User.prototype.alertAge = function(){//公共方法,也叫原型方法 alert(this.age); } User.prototype.sex = '男';//原型屬性 User.getName= function(){//靜態(tài)方法 return this.myname; } User.getAge = function(){ this.alertAge(); } User.getDo = function(){ return this.do(); } //console.log(User.getName())//undefined //console.log(User.getDo());//TypeError: this.do is not a function //console.log(User.getAge())//TypeError: this.alertAge is not a function
8.特權(quán)方法的調(diào)用規(guī)則
特權(quán)方法通過this調(diào)用公有方法、公有屬性,通過對象本身調(diào)用靜態(tài)方法和屬性,在方法體內(nèi)直接調(diào)用私有屬性和私有方法
function User(girlfriend){ var girlfriend = girlfriend; function getGirlFriend(){ return '我女朋友'+girlfriend+'是美女!'; } this.myname = 'fire子海';//公有屬性 this.age = 26; this.do = function(){//特權(quán)方法 return this.myname+'學(xué)習(xí)js'; } this.alertAge = function(){ this.changeAge();//特權(quán)方法調(diào)用公有方法 alert(this.age); } this.alertGirlFriend = function(){ alert(getGirlFriend());//調(diào)用私有方法 } } User.prototype.changeAge = function(){ this.age = 29; } var user = new User('某某'); user.alertAge();//alert:29 user.alertGirlFriend();//alert:我的女朋友某某是美女!
9.私有方法
對象的私有方法和屬性,外部是不可以訪問的,在方法的內(nèi)部不是能this調(diào)用對象的公有方法、公有屬性、特權(quán)方法的
function User(girlfriend){ var girlfriend = girlfriend; this.myname = 'fire子海';//公有屬性 this.age = 26; function getGirlFriend(){ //this.myname ;//此時(shí)的this指向的window對象,并非User對象, // this.myname = 'fire子海',此時(shí)的this指向的是getGirFriend對象了。 //如果通過this調(diào)用了getGirFriend中不存在的方法呀屬性,this便會(huì)指向window 對象,只有this調(diào)用了getGirlFriend存在的方法和屬性,this才會(huì)指定getGirlFriend; alert(User.eat('泡面'));//alert:晚餐只有方便面 } this.do = function(){//特權(quán)方法 return this.myname+'學(xué)習(xí)js'; } this.alertAge = function(){ this.changeAge();//特權(quán)方法調(diào)用公有方法 alert(this.age); } this.alertGirlFriend = function(){ getGirlFriend();//調(diào)用私有方法 } } User.eat = function(supper){ return '晚餐只有'+supper; } var user = new User('某某'); user.alertGirlFriend();
以上所述就是本文的全部內(nèi)容了,希望大家能夠喜歡。
- JavaScript面向?qū)ο笾叽蠡驹瓌t實(shí)例詳解
- JavaScript面向?qū)ο笕齻€(gè)基本特征實(shí)例詳解【封裝、繼承與多態(tài)】
- JS面向?qū)ο缶幊獭狤S6 中class的繼承用法詳解
- JS面向?qū)ο蠡A(chǔ)講解(工廠模式、構(gòu)造函數(shù)模式、原型模式、混合模式、動(dòng)態(tài)原型模式)
- 淺談Javascript面向?qū)ο缶幊?/a>
- 從面試題學(xué)習(xí)Javascript 面向?qū)ο螅▌?chuàng)建對象)
- JAVASCRIPT THIS詳解 面向?qū)ο?/a>
- JS面向?qū)ο缶幊虦\析
- JavaScript面向?qū)ο蠛诵闹R與概念歸納整理
相關(guān)文章
JavaScript模運(yùn)算符理解及運(yùn)用實(shí)戰(zhàn)
這篇博客文章是為初級到中級JavaScript開發(fā)人員所寫,主要為大家介紹了JavaScript模運(yùn)算符理解及運(yùn)用實(shí)戰(zhàn)示例,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步2023-11-11JS 密碼強(qiáng)度驗(yàn)證(兼容IE,火狐,谷歌)
仿JQuery中文社區(qū)注冊,JS 密碼強(qiáng)度驗(yàn)證(兼容IE,火狐,谷歌)2010-03-03es6學(xué)習(xí)筆記之Async函數(shù)基本教程
這篇文章主要給大家介紹了關(guān)于es6中Async函數(shù)的基本教程,文中介紹的非常詳細(xì),對大家學(xué)習(xí)async函數(shù)具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧。2017-05-05jscript之List Excel Color Values
jscript之List Excel Color Values...2007-06-06