js中公有方法、特权方法、静态方法
1.公有屬性和公有方法
| 1 2 3 4 5 6 7 8 9 | 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.私有屬性和方法
| 1 2 3 4 5 6 7 8 9 | 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中,無(wú)需實(shí)例化就可以調(diào)用的方法就叫靜態(tài)方法,js也一樣,無(wú)需實(shí)例化,即用new操作符實(shí)化對(duì)象,就可調(diào)用對(duì)象的方法和屬性。
| 1 2 3 4 5 6 7 8 | 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)方法
| 1 2 3 4 5 6 7 8 9 | 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)類
對(duì)于靜態(tài)方法和靜態(tài)屬性,我們無(wú)需像第三步中那樣去創(chuàng)建,如果網(wǎng)友看過(guò)我那篇“js如何制作圖片輪播”,就知道可以使用字面量的方式來(lái)創(chuàng)建。
| 1 2 3 4 5 6 7 8 9 10 11 | 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í)例化對(duì)象
公有方法中通過(guò)不this調(diào)用公有屬性和特權(quán)方法,不能使用this調(diào)用靜態(tài)方法和屬性,必需裁通過(guò)對(duì)象本身調(diào)用,即對(duì)象名。公有方法也不能調(diào)用私有方法
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | 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));//只能通過(guò)對(duì)象本身調(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í),無(wú)需實(shí)例化對(duì)象,便可以調(diào)用,對(duì)象實(shí)例不能調(diào)用對(duì)象的靜態(tài)方法,只能調(diào)用實(shí)例自身的靜態(tài)屬性和方法
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 | 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)方法無(wú)法調(diào)用公有屬性、公有方法、私有方法、私有屬性、特權(quán)方法和原型屬性
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | 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)方法通過(guò)this調(diào)用公有方法、公有屬性,通過(guò)對(duì)象本身調(diào)用靜態(tài)方法和屬性,在方法體內(nèi)直接調(diào)用私有屬性和私有方法
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | 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.私有方法
對(duì)象的私有方法和屬性,外部是不可以訪問(wèn)的,在方法的內(nèi)部不是能this調(diào)用對(duì)象的公有方法、公有屬性、特權(quán)方法的
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 | function User(girlfriend){ ???var girlfriend = girlfriend; ??this.myname = 'fire子海';//公有屬性 ??this.age = 26; ??function getGirlFriend(){ ???//this.myname ;//此時(shí)的this指向的window對(duì)象,并非User對(duì)象, ????// this.myname = 'fire子海',此時(shí)的this指向的是getGirFriend對(duì)象了。 ??//如果通過(guò)this調(diào)用了getGirFriend中不存在的方法呀屬性,this便會(huì)指向window 對(duì)象,只有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(); |
轉(zhuǎn)載于:https://www.cnblogs.com/wang98/p/7652644.html
總結(jié)
以上是生活随笔為你收集整理的js中公有方法、特权方法、静态方法的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: react+redux实战——redux
- 下一篇: 浅析JWT| JWT是啥子,Java构建