js里面的属性和方法类型
来源:互联网 发布:淘宝客佣金代扣款给谁 编辑:程序博客网 时间:2024/05/16 17:06
js--属性和方法(私有/公有)
【私有变量】 在对象内部使用'var'关键字来声明,而且它只能被私有函数和特权方法访问。
【私有方法】 在对象的构造函数里声明(或者是通过varfunctionName=function(){...}来定义),
它能被特权方法调用(包括对象的构造方法)和私有方法调用,私有函数只能访问私有的方法和属性。
【特权方法】通过this.methodName=function(){...}来声明而且可能被对象外部的代码调用。
它可以使用:this.特权函数() 方式来调用特权函数,使用 :私有函数()方式来调用私有函数。
【公共属性】 通过this.variableName来定义而且在对象外部是可以读写的。不能被私有函数所调用。
【公共方法】 通过ClassName.prototype.methodName=function(){...}来定义可以从对象外部来调用。
【原型属性】 通过ClassName.prototype.propertyName=someValue 来定义。
【静态属性】 通过ClassName.propertyName=someValue 来定义。
【静态方法】 通过ClassName.funName=function(){...} 来定义。
1
function
myfun1(){
2
//这是私有属性
3
var
private1 =
"这是私有属性"
;
4
var
privateMethod =
function
(){
5 alert(private1);
6 }
7
//这是实例属性
8
this
.publicvar =
"这是实例属性"
;
9
this
.public1 =
function
(){
10 privateMethod();
11 }
12 }
1
var
newfun1 =
new
myfun1();
2 newfun1.public1();
//这是私有属性
3 alert(newfun1.publicvar);
//这是实例属性
4 alert(newfun1.private1);
// undefined newfun1.privateMethod(); //运行错误
1
function
myfun2(){
2 }
3 myfun2.staticvar =
"这是静态属性"
;
4 myfun2.staticmethod =
function
(){
5 alert(myfun2.staticvar);
6 }
7
var
newfun2 =
new
myfun2();
8
//newfun2.staticmethod();//运行错误;
9 alert(newfun2.staticvar);
//undefined
1
//静态私有成员
2
var
myfun3 = (
function
(){
3
function
privateProperty(){
4
5 }
6 privateProperty.staticvar =
"这是静态私有成员"
;
7 privateProperty.staticmethod =
function
(){
8 alert(privateProperty.staticvar);
9 }
10 privateProperty.staticmethod();
11
return
privateProperty
12 })();
1
//静态类
2
var
funcount = 0;
3
var
myfun4 =
new
function
(){
4 funcount++;
5
this
.printCount =
function
(){
6 alert(funcount);
7 }
8 }
9 myfun4.printCount();
//输出1;
10 myfun4.printCount();
//输出1;
11 myfun4.prototype.amethod =
function
(){
12 alert(
"原型对象"
);
13 }
//运行错误
14
var
newfun4 =
new
myfun4();
15 newfun4.amethod();
1
//运行错误,说明myfun3创建并实例化之后就不能再为它添加方法,属性
2
new
myfun3.constructor().printCount();
//如果你确实想实例化,这样也可以.
3
//原型继承
4
var
myfun5 =
function
(){
5
6 }
7 myfun5.prototype.myfun5_extend =
function
(){
8 alert(
"这是原型继承的"
);
9 }
10
var
myfun5_sub =
function
(){
11
12 }
13 myfun5_sub.prototype =
new
myfun5();
14
var
newfun5 =
new
myfun5_sub();
15 newfun5.myfun5_extend();
//这是原型继承的
16
//调用继承
17
var
myfun6 =
function
(){
18
this
.method_p =
function
(){
19 alert(
"这是调用继承的"
);
20 }
21 }
22
var
myfun6_sub =
function
(){
23 myfun6.call(
this
);
24 }
25
26
var
newfun6 =
new
myfun6_sub();
27 newfun6.method_p();
//这是调用继承的
28
//覆盖
29
var
myfun7 =
function
(){
30
this
.method =
function
(){
31 alert(
"这是父对象方法"
);
32 }
33 }
34
var
myfun7_sub =
function
(){
35
this
.method =
function
(){
36 alert(
"这是子对象方法"
);
37 }
38 }
39 myfun7_sub.prototype =
new
myfun7();
40
var
newfun7 =
new
myfun7_sub();
41 newfun7.method();
//这是子对象方法,父对象方法被覆盖了.
42
//多态
43
function
myfun8(a, b){
44
var
a = a;
45
var
b = b;
46
if
(
typeof
a ==
"number"
&&
typeof
b ==
"number"
) {
47 alert(a * b);
48 }
else
if
(
typeof
a ==
"string"
&&
typeof
b ==
"string"
) {
49 alert(a + b);
50 }
else
{
51 alert(
"输入错啦"
);
52 }
53 }
54
55 myfun8(3, 4);
// 输出12;
56 myfun8(
"hi,"
,
"你好"
);
//输出hi,你好;
57 myfun8(
"hi"
, 5);
//输入错啦.
0 0
- js里面的属性和方法类型
- js中DOM里面的属性,方法
- UITableView里面的属性和方法总结
- js里面的实例方法和静态方法
- Java 得到一个类里面的方法和属性
- String类型的属性和方法
- String类型的属性和方法(一)
- String类型的属性和方法(二)
- String类型的属性和方法(三)
- activex里面的没有CString属性类型
- js属性和方法的可见性:私有属性(方法),实例属性(方法),类属性(方法)
- js的document的属性和方法
- js里面window.open方法的详细定义和用法
- JS的Document属性和方法 (抄)
- JS的Document属性和方法 (抄)
- JS Window对象的属性和方法
- JS的Document属性和方法
- JS的Document属性和方法
- [leetcode] Evaluate Reverse Polish Notation
- UNIX环境高级编程学习笔记(六)标准I/O库
- Android App的启动过程
- DataSnap服务器如何得到客户端的IP和端口
- 【MIG专项测试组】如何准确评测Android应用的流畅度?
- js里面的属性和方法类型
- SSL基础
- 黑马程序员--数组常用排序方法
- java门禁系统面向对象程序设计
- Java并发编程-03-守护线程的创建和运行
- SpringMVC视图解析器(1)
- MySQL:ERROR 2002 (HY000)
- Centeros 系统下error while loading shared libraries:libmysqlclient.so.18错误
- 【Swift】text修改内容