整理JS的prototyp,call用法深入理解
来源:互联网 发布:学网店美工 编辑:程序博客网 时间:2024/05/18 03:35
JavaScript能够实现的面向对象的特征有:
·公有属性(public field)
·公有方法(public Method)
·私有属性(private field)
·私有方法(private field)
·方法重载(method overload)
·构造函数(constructor)
·事件(event)
·单一继承(single inherit)
·子类重写父类的属性或方法(override)
小结:prototype在这里相当于在类继承父类,格式为"类名.protype"
{
this.name=name;
//对象方法
this.Introduce=function(){
alert("My name is "+this.name);
}
}
//类方法
People.Run=function(){
alert("I can run");
}
//原型方法
People.prototype.IntroduceChinese=function(){
alert("我的名字是"+this.name);
}
//测试
var p1=new People("Windking");
p1.Introduce();
People.Run();
p1.IntroduceChinese();
·公有属性(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
Js代码
- <script
type= "text/javascript"> - Object.prototype.Property
= 1; - Object.prototype.Method
= function() - {
alert(1); - }
- var
obj new= Object(); - alert(obj.Property);
- obj.Method();
- </script>
例子二(prototype使用的限制):在实例上不能使用prototype,否则发生编译错误
Js代码
- <script
type= "text/javascript"> - var
obj new= Object(); - obj.prototype.Property
= //Error1; - //Error
- obj.prototype.Method
= function() - {
alert(1); - }
- </script>
例子十(如何让一个类型继承于另一个类型):这个例子说明了一个类型如何从另一个类型继承。
Js代码
- <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>
以上参考地址:http://blog.csdn.net/tianyitianyi1/article/details/6929916
javascript的方法可以分为三类:
a 类方法
b 对象方法
c 原型方法
例子:
function People(name)//这里相等于定义了一个People类哦{
this.name=name;
//对象方法
this.Introduce=function(){
alert("My name is "+this.name);
}
}
//类方法
People.Run=function(){
alert("I can run");
}
//原型方法
People.prototype.IntroduceChinese=function(){
alert("我的名字是"+this.name);
}
//测试
var p1=new People("Windking");
p1.Introduce();
People.Run();
p1.IntroduceChinese();
function People(name){this.name=name;//对象方法this.Introduce=function(){alert("My name is "+this.name);}}//类方法People.Run=function(){alert("I can run");}//原型方法,个人认为就是构造对象时就产生的方法People.prototype.IntroduceChinese=function(){alert("我的名字是"+this.name);} <script type="text/javascript">function baseClass(){ this.showMsg = function() { alert("baseClass::showMsg"); } this.baseShowMsg = function() { alert("baseClass::baseShowMsg"); }}baseClass.showMsg = function(){ alert("baseClass::showMsg static");}function extendClass(){ this.showMsg =function () { alert("extendClass::showMsg"); }}extendClass.showMsg = function(){ alert("extendClass::showMsg static")}extendClass.prototype = new baseClass();var instance = new extendClass();instance.showMsg(); //显示extendClass::showMsginstance.baseShowMsg(); //显示baseClass::baseShowMsginstance.showMsg(); //显示extendClass::showMsgbaseClass.showMsg.call(instance);//显示baseClass::showMsg staticvar baseinstance = new baseClass();baseinstance.showMsg.call(instance);//显示baseClass::showMsg</script>//测试var p1=new People("Windking");p1.Introduce();People.Run();p1.IntroduceChinese();obj1.func.call(obj)方法意思是将obj看成obj1,调用func方法<span style="color:#FF0000;"><strong>这个理解很关键,意思是我最后肯定是执行obj1.func的方法,这点无须质疑,但是obj1会被替换成obj</strong></span> onDblClickRow:function(rowIndex, rowData){//双击回传数据 var row_dialog = $('#damageDialog').datagrid("getSelected"); var documents = getFramesDom($('#formCode').val()); documents[0][callbackFunc].call(this); } 通过拿到父页面的dom,然后拿到注入到dom中的函数,通过执行加强练习<script type="text/javascript">function baseClass(){ this.showMsg = function() { alert("baseClass::showMsg"); } this.baseShowMsg = function() { alert("baseClass::baseShowMsg"); }}baseClass.showMsg = function(){ alert("baseClass::showMsg static");}function extendClass(){ this.showMsg =function () { alert("extendClass::showMsg"); }}extendClass.showMsg = function(){ alert("extendClass::showMsg static")}extendClass.prototype = new baseClass();var instance = new extendClass();instance.showMsg(); //显示extendClass::showMsginstance.baseShowMsg(); //显示baseClass::baseShowMsginstance.showMsg(); //显示extendClass::showMsgbaseClass.showMsg.call(instance);//显示baseClass::showMsg staticvar baseinstance = new baseClass();baseinstance.showMsg.call(instance);//显示baseClass::showMsg</script>参考地址:http://www.cnblogs.com/yjf512/archive/2011/06/03/2071914.html
0 0
- 整理JS的prototyp,call用法深入理解
- JS中call用法理解
- JS的Call用法
- 深入理解js中的apply、call、bind
- js里面call的用法
- JS中call的用法
- call和apply的深入理解
- 关于js中call方法的理解
- JS中apply与call的用法
- js中call和apply的用法
- JS中apply与call的用法
- JS中apply与call的用法
- js中call和apply的用法
- 【js】apply和call的用法
- JS call和apply的用法
- js中 call方法的用法
- !!js中call、apply、bind的用法
- js中call与apply的用法
- 【旋转矩阵】Rotate Image
- linux win7双系统
- OC block的简单使用
- 黑马程序员_java基础知识(5)枚举的简单理解
- POJ 1733 Parity game(离散化+带权并查集)
- 整理JS的prototyp,call用法深入理解
- 在ubuntu系统上,beagleboneblack借用usb实现网络与主机共享
- 《富爸爸商学院》读书笔记(一)
- Ubuntu安装MySQL
- express: command not found.
- C#动态添加label控件
- 学习笔记1(widbg)
- 第六周作业2——哈夫曼编码实现
- linux给文件或目录添加apache权限