JS- 封装、继承、多态

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);//输入错啦.
58
复制代码
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);//输入错啦.
58
复制代码

### JavaScript中的封装继承多态 #### 封装 在JavaScript中,封装意味着隐藏对象内部状态并仅暴露有限的访问途径。这通常通过闭包或利用`class`关键字定义的方法与属性来实现。对于旧版本浏览器兼容性考虑,则更多依赖于构造函数模式。 ```javascript function Person(name) { // 私有成员 const privateName = name; // 公共方法用于获取私有数据 this.getName = function() { return privateName; }; } ``` 上述代码展示了如何创建具有私有属性的对象实例[^4]。这里`privateName`作为局部变量存在于构造器作用域内,外部无法直接访问它;而`getName()`则提供了一种安全的方式读取该值。 #### 继承 JavaScript主要采用基于原型链的形式完成类间关系建立。一种常见做法是指定子类原型指向父类新实例从而获得其全部特性: ```javascript // 定义超类型(父类) function Animal(sound){ this.makeSound = function(){ console.log(sound); } } // 子类型的构造函数 function Dog(breed, sound){ Animal.call(this, sound); // 调用超类型的构造函数 this.breed = breed; } Dog.prototype = Object.create(Animal.prototype); // 创建新的原型对象关联到父级 Dog.prototype.constructor = Dog; // 修复constructor指针 ``` 此片段说明了怎样让一个特定种类如狗能够继承动物的行为特征的同时还拥有自己独有的属性——品种信息[^1][^3]。 #### 多态 多态允许不同类别的实体以统一接口呈现出来,在运行时决定具体执行哪个版本的操作。下面的例子体现了这一点: ```javascript Person.prototype.walk = function(){ console.log(`${this.name} is walking.`); }; Student.prototype = new Person(); Student.prototype.study = function(){ console.log(`${this.name} studies hard.`); }; let studentInstance = new Student("Alice"); studentInstance.walk(); // Alice is walking. studentInstance.study(); // Alice studies hard. ``` 这段脚本里,尽管`walk()`最初是在`Person`级别定义好的通用行为,但当应用于更具体的派生类别比如学生身上时同样适用,并且各自还可以扩展额外功能[^5]。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值