js中prototype用法(转)
JavaScript能够实现的面向对象的特征有:
·公有属性(public field)
·公有方法(public Method)
·私有属性(private field)
·私有方法(private field)
·方法重载(method overload)
·构造函数(constructor)
·事件(event)
·单一继承(single inherit)
·子类重写父类的属性或方法(override)
·静态属性或方法(static member)
例子一(JavaScript中允许添加行为的类型):可以在类型上使用proptotype来为类型添加行为。这些行为只能在类型的实例上体现。 JS中允许的类型有Array, Boolean, Date, Enumerator, Error, Function, Number, Object, RegExp, String
- <script
type= "text/javascript"> - Object.prototype.Property
= 1; - Object.prototype.Method
= function() - {
alert(1); - }
- var
obj new= Object(); - alert(obj.Property);
- obj.Method();
- </script>
- <script
type= "text/javascript"> - var
obj new= Object(); - obj.prototype.Property
= //Error1; - //Error
- obj.prototype.Method
= function() - {
alert(1); - }
- </script>
- <script
type= "text/javascript"> - Object.Property
= 1; - Object.Method
= function() - {
alert(1); - }
- alert(Object.Property);
- Object.Method();
- </script>
- <script
type= "text/javascript"> - function
Aclass() - {
- this.Property
= 1; - this.Method
= function() - {
alert(1); - }
- }
- var
obj new= Aclass(); - alert(obj.Property);
- obj.Method();
- </script>
- <script
type= "text/javascript"> - function
Aclass() - {
- this.Property
= 1; - this.Method
= function() - {
alert(1); - }
- }
- Aclass.prototype.Property2
= 2; - Aclass.prototype.Method2
= function - {
alert(2); - }
- var
obj new= Aclass(); - alert(obj.Property2);
- obj.Method2();
- </script>
- <script
type= "text/javascript"> - function
Aclass() - {
- this.Property
= 1; - this.Method
= function() - {
alert(1); - }
- }
- var
obj new= Aclass(); - obj.Property
= 2; - obj.Method
= function() - {
alert(2); - }
- alert(obj.Property);
- obj.Method();
- </script>
- <script
type= "text/javascript"> - function
Aclass() - {
- this.Property
= 1; - this.Method
= function() - {
alert(1); - }
- }
- var
obj new= Aclass(); - obj.Property3
= 3; - obj.Method3
= function() - {
alert(3); - }
- alert(obj.Property3);
- obj.Method3();
- </script>
- <script
type= "text/javascript"> - function
AClass() - {
this.Property= 1; this.Method= function(){ alert(1); } - }
- function
AClass2() - {
this.Property2= 2; this.Method2= function(){ alert(2); } - }
- AClass2.prototype
= newAClass(); - var
obj new= AClass2(); - alert(obj.Property);
- obj.Method();
- alert(obj.Property2);
- obj.Method2();
- </script>
- <script
type= "text/javascript"> - function
AClass() - {
this.Property= 1; this.Method= function(){ alert(1); } - }
- function
AClass2() - {
this.Property2= 2; this.Method2= function(){ alert(2); } - }
- AClass2.prototype
= newAClass(); - AClass2.prototype.Property
= 3; - AClass2.prototype.Method
= function() - {
alert(4); - }
- var
obj new= AClass2(); - alert(obj.Property);
- obj.Method();
- </script>
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· .NET10 - 预览版1新功能体验(一)