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.静态属性和方法
在php中,无需实例化就可以调用的方法就叫静态方法,js也一样,无需实例化,即用new操作符实化对象,就可调用对象的方法和属性。
1
2
3
4
5
6
7
8
|
function User(){}
User.age = 26; //静态属性
User.myname = 'fire子海' ;
User.getName = function (){ //静态方法
return this .myname; //如果这里使用this.name,返回的将是User,所有改用了myname,
}
console.log(User.getName()); //output:fire子海
|
4.特权方法
1
2
3
4
5
6
7
8
9
|
function User(name,age){
var name = name; //私有属性
var age = age;
this .getName = function (){ //特权方法
return name; //私有属性和方法不能使用this调用
}
}
var user = new User( 'fire子海' ,26);
console.log(user.getName()); //output:fire子海
|
5.静态类
对于静态方法和静态属性,我们无需像第三步中那样去创建,如果网友看过我那篇“js如何制作图片轮播”,就知道可以使用字面量的方式来创建。
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.公有方法的调用规则
调用公有方法,我们必需先实例化对象
公有方法中通过不this调用公有属性和特权方法,不能使用this调用静态方法和属性,必需裁通过对象本身调用,即对象名。公有方法也不能调用私有方法
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 (){ //特权方法
return this .myname+ '学习js' ;
}
}
User.eat = function (food){
return '晚餐只有' +food;
}
User.prototype.alertAge = function (){
alert( this .age);
}
User.prototype.alertDo = function (){
alert( this . do ()); //调用特权方法
}
User.prototype.alertEat = function (food){
alert(User.eat(food)); //只能通过对象本身调用静态方法
//alert(this.ear(food))这样调用将出错:this.eat is not a function
}
var user = new User();
user.alertAge(); //alert:26
user.alertDo(); //alert:fire子海学习js
user.alertEat( '方便面' ) //alert:晚餐只有方便面
|
7.静态方法的调用规则
使用静态方法时,无需实例化对象,便可以调用,对象实例不能调用对象的静态方法,只能调用实例自身的静态属性和方法
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
function User(){}
User.age = 26; //静态属性
User.myname = 'fire子海' ;
User.getName = function (){ //静态方法
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(); //晚餐只有方便面
|
静态方法无法调用公有属性、公有方法、私有方法、私有属性、特权方法和原型属性
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 (){ //特权方法
return this .myname+ '学习js' ;
}
}
User.prototype.alertAge = function (){ //公共方法,也叫原型方法
alert( this .age);
}
User.prototype.sex = '男' ; //原型属性
User.getName= function (){ //静态方法
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.特权方法的调用规则
特权方法通过this调用公有方法、公有属性,通过对象本身调用静态方法和属性,在方法体内直接调用私有属性和私有方法
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 (){ //特权方法
return this .myname+ '学习js' ;
}
this .alertAge = function (){
this .changeAge(); //特权方法调用公有方法
alert( this .age);
}
this .alertGirlFriend = function (){
alert(getGirlFriend()); //调用私有方法
}
}
User.prototype.changeAge = function (){
this .age = 29;
}
var user = new User( '某某' );
user.alertAge(); //alert:29
user.alertGirlFriend(); //alert:我的女朋友某某是美女!
|
9.私有方法
对象的私有方法和属性,外部是不可以访问的,在方法的内部不是能this调用对象的公有方法、公有属性、特权方法的
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 ;//此时的this指向的window对象,并非User对象,
// this.myname = 'fire子海',此时的this指向的是getGirFriend对象了。
//如果通过this调用了getGirFriend中不存在的方法呀属性,this便会指向window 对象,只有this调用了getGirlFriend存在的方法和属性,this才会指定getGirlFriend;
alert(User.eat( '泡面' )); //alert:晚餐只有方便面
}
this . do = function (){ //特权方法
return this .myname+ '学习js' ;
}
this .alertAge = function (){
this .changeAge(); //特权方法调用公有方法
alert( this .age);
}
this .alertGirlFriend = function (){
getGirlFriend(); //调用私有方法
}
}
User.eat = function (supper){
return '晚餐只有' +supper;
}
var user = new User( '某某' );
user.alertGirlFriend();
|
转载于:https://www.cnblogs.com/wang98/p/7652644.html
js中公有方法、特权方法、静态方法相关推荐
- js中match、replace方法中使用正则表达式
为什么80%的码农都做不了架构师?>>> js中match.replace方法中使用正则表达式 (2012-06-29 14:23:37) 转载▼ 标签: 字符串 飞雪 正则表 ...
- JS中令人发指的valueOf方法介绍
转载自 JS中令人发指的valueOf方法介绍 彭老湿近期月报里提到了valueOf方法,兴致来了翻了下ECMA5里关于valueOf方法的介绍,如下: 15.2.4.4 Object.protot ...
- vue 懒人_Vue.js 中的实用工具方法【推荐】
收集日常开发中常用到的一些工具方法, 包含 vue 的公用过滤器.公用指令等 (PS: 懒人养成记) 公用自定义过滤器 import Vue from 'vue' import moment from ...
- java有push方法么_[Java教程]js中push和join方法使用介绍
[Java教程]js中push和join方法使用介绍 0 2013-10-09 07:00:17 push和join方法想必大家并不陌生吧,在本文将为大家详细介绍下js中的push和join方法的使用 ...
- js中数组常用的方法总结,包括ES6
原文地址:js中数组常用的方法总结,包括ES6 1.push() 后增 push()方法可以向数组后添加一个新的元素,并返回新数组的长度. 末尾添加,返回长度,改变原数组 var a = [1,2,3 ...
- js中判断数据类型的方法
JS中判断数据类型的方法: 使用typeof操作符. 对一个值使用 typeof 操作符可能返回下列某个字符串,返回的类型都是字符串形式. (1) undefined:如果这个值未定义 (2) boo ...
- JS中 new Date() 各方法的用法
JS中 new Date() 各方法的用法 1.new Date() 参数篇 a.返回类型为国标时间, b.无参数时可以直接返回输出时的时间, c.有参数时则返回对应时间的国标时间, d.日期中间的符 ...
- JS中数组使用的方法
JS中数组使用的方法 数组的介绍 JavaScript Array(数组):属于对象的一种 在句子中的作用就是使用一个单独的变量来存储一系列的值 数组的创建方法 1.字面量方式:var arr=[] ...
- java和js中替换字符串的方法
//java中字符串的替换方法: String a = "吴林飞-1a_,唐姐-2,周帅-3"; String f = a.replaceFirst("-", ...
- JS中所有遍历的方法
JS中所有遍历的方法 1.for for(let i=0;i<len;i++){//do something} 2.forEach forEach 循环不支持return实例:let arr = ...
最新文章
- Prototype和Scriptaculous圣经——《Ajax实战:Prototype与Scriptaculous篇》
- shell的最大命令行长度
- 伪代码的简单例子_使用策略+工厂模式彻底干掉代码中的if else!
- (七)立体标定与立体校正 【计算机视觉学习笔记--双目视觉几何框架系列】
- C#多线程编程系列(二)- 线程基础
- centOS 6 rpm安装MySQL小记
- 使用FreeImage加载纹理
- RESTful架构与RPC架构
- Hibernate在配置表映射文件时cascade的类型及意义
- 百度网盘 自定义分享密码 提取码
- NODDI在临床研究中的应用
- 【愚公系列】2022年10月 微信小程序-电商项目-商品详情页面的标题及价格功能实现
- android 自定义控件github,GitHub - swordman20/AndroidDIYWidget: Android组合自定义控件
- 不花冤枉钱就能回家 --- 程序员教你如何高效抢票
- 大数据分析」最详细的大数据分析师技能图谱详解与零基础自学内容大全
- 使用Bandwagon服务器ftp解决git clone速度慢的问题
- 爬虫基础篇之IP代理池
- Metal 系列教程
- 什么是CRUD( What is CRUD)?
- Matlab对科氏力建模,基于ADAMS和MATLAB的惯性组件联合仿真研究
热门文章
- Java程序完形填空题_完形填空:-_JavaDeveloper-51CTO博客
- Java学习笔记---Lambda表达式及Stream流Api
- IDEA注释模板设置【非常实用】
- Scroller类源码解析及其应用(一)
- 20180402-B · US Tuition Costs · ggplot2, 条线图 柱状图 · R 语言数据可视化 案例 源码
- ArcGIS Server Rest 认证过程分析
- 深入解读 Spark 宽依赖和窄依赖(ShuffleDependency NarrowDependency)
- 开发杂谈:后移动互联网时代我的一些思考
- 多重利好袭来,“东数西算”的背后意义深远
- ZIP压缩算法原理解析(好文推荐,看完就懂)