整理JS的prototyp,call用法深入理解

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

Js代码
  1. <script type="text/javascript">
  2. Object.prototype.Property = 1;
  3. Object.prototype.Method = function()
  4. {
  5. alert(1);
  6. }
  7. varobj=newObject();
  8. alert(obj.Property);
  9. obj.Method();
  10. </script>

例子二(prototype使用的限制):在实例上不能使用prototype,否则发生编译错误

Js代码
  1. <script type="text/javascript">
  2. varobj=newObject();
  3. obj.prototype.Property = 1; //Error
  4. //Error
  5. obj.prototype.Method = function()
  6. {
  7. alert(1);
  8. }
  9. </script>
小结:prototype在这里相当于在类的构造方法中为某个字段赋值,格式为"类名.protype.属性名"


例子十(如何让一个类型继承于另一个类型):这个例子说明了一个类型如何从另一个类型继承。

Js代码
  1. <script type="text/javascript">
  2. functionAClass()
  3. {
  4. this.Property=1;
  5. this.Method=function()
  6. {
  7. alert(1);
  8. }
  9. }
  10. functionAClass2()
  11. {
  12. this.Property2=2;
  13. this.Method2=function()
  14. {
  15. alert(2);
  16. }
  17. }
  18. AClass2.prototype = newAClass();
  19. varobj=newAClass2();
  20. alert(obj.Property);
  21. obj.Method();
  22. alert(obj.Property2);
  23. obj.Method2();
  24. </script>
小结:prototype在这里相当于在类继承父类,格式为"类名.protype"

以上参考地址: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::showMsg
instance.baseShowMsg(); //显示baseClass::baseShowMsg
instance.showMsg(); //显示extendClass::showMsg

baseClass.showMsg.call(instance);//显示baseClass::showMsg static

var 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::showMsg
instance.baseShowMsg(); //显示baseClass::baseShowMsg
instance.showMsg(); //显示extendClass::showMsg

baseClass.showMsg.call(instance);//显示baseClass::showMsg static

var baseinstance = new baseClass();
baseinstance.showMsg.call(instance);//显示baseClass::showMsg

</script>



参考地址:http://www.cnblogs.com/yjf512/archive/2011/06/03/2071914.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值