js类成员笔记

本文介绍JavaScript中的多种设计模式,包括公共、私有及特权方法的使用,静态方法的应用,以及如何通过构造函数创建实例方法。文章还展示了字符串修剪等实用功能的具体实现。

 

ContractedBlock.gifExpandedBlockStart.gif
ExpandedBlockStart.gifContractedBlock.gif/**//*
InBlock.gifPatterns模式
InBlock.gifPublic公共
InBlock.giffunction Constructor(dot.gif
InBlock.gif
InBlock.gif    this.membername = value;
InBlock.gif}
InBlock.gifConstructor.prototype.membername = value;
InBlock.gif
InBlock.gifPrivate私有
InBlock.giffunction Constructor(dot.gif
InBlock.gif
InBlock.gif    var self = this;
InBlock.gif    var membername = value; 
InBlock.gif    function membername(dot.gif) {dot.gif}
InBlock.gif}
InBlock.gif
InBlock.gifNote: The function statement
InBlock.giffunction membername(dot.gif) {dot.gif}
InBlock.gif
InBlock.gifis shorthand for
InBlock.gifvar membername = function membername(dot.gif) {dot.gif};
InBlock.gif
InBlock.gifPrivileged特权 一个特权A privileged 方法可以访问私有的变量和方法,同时它对公共域可见。也可以删除或替换一个特权方法,但不能改变它。
InBlock.gif
InBlock.gif特权方法是用 this 在构造器中分配的
InBlock.gif
InBlock.giffunction Constructor(dot.gif
InBlock.gif
InBlock.gif    this.membername = function (dot.gif)
InBlock.gif    {
InBlock.gif        self.membernamedot.gifdot.gif;
InBlock.gif    };
InBlock.gif}
InBlock.gif
InBlock.gifStatic 
InBlock.gifConstructor.Method = function()
InBlock.gif{
InBlock.gif    this.memberdot.gif;
InBlock.gif}
InBlock.gif
InBlock.gifAdd public Method;
InBlock.gif这个技巧一般用来添加公共方法。当一个成员被检索且没有在对象中发现的时候,那么它就会从对象构造器的 prototype 成员中去获取它。
InBlock.gif这个原型机制可用来实现继承。它也保存内存。要为一个构造器生成的所有对象加入一个方法,将函数加入构造器的prototype 中:
InBlock.gifContainer.prototype.myMethod = function (dot.gif
InBlock.gif{
InBlock.gif    this.memberdot.gif;
InBlock.gif}
InBlock.gif
InBlock.gif
ExpandedBlockEnd.gif
*/

None.gif
None.gif
//class MyString
None.gif
function MyString(strs)
ExpandedBlockStart.gifContractedBlock.gif
dot.gif{    
InBlock.gif    
var _str = strs;//private field;
InBlock.gif
    this.Str = _str;//public field
InBlock.gif
    
InBlock.gif    
InBlock.gif    
//public method
InBlock.gif
    this.leftTrim = function(start)       
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{        
InBlock.gif        ret 
= leftTrim(this.Str,start);       //this=new MyString(strs); 
InBlock.gif        
return ret;
ExpandedSubBlockEnd.gif    }

InBlock.gif    
InBlock.gif    
//private method
InBlock.gif
    function leftTrim(str,start)          
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{        
InBlock.gif        
if( str.length>0 && str.indexOf(start)==0 )
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            str 
= str.substring(start.length,str.length);        
InBlock.gif            str 
= leftTrim(str,start);            
ExpandedSubBlockEnd.gif        }
    
InBlock.gif        
return str;
ExpandedSubBlockEnd.gif    }
    
InBlock.gif    
InBlock.gif    
this.rightTrim = function(end)
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        ret 
= rightTrim(_str,end);
            return ret;
ExpandedSubBlockEnd.gif    }
    
InBlock.gif    
InBlock.gif    
function rightTrim(str,end)
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
if( str.length>0 && str.lastIndexOf(end) == str.length-end.length )
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            str 
= str.substring(0,str.length-end.length);        
InBlock.gif            str 
= rightTrim(str,end);
ExpandedSubBlockEnd.gif        }

InBlock.gif        
return str;
ExpandedSubBlockEnd.gif    }

InBlock.gif    
InBlock.gif    
var self = this;
InBlock.gif    
this.Trim = function()
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        str 
= rightTrim(self.leftTrim(" ")," ");
InBlock.gif        
InBlock.gif        return str
;
ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}

None.gif
None.gif
//static method
None.gif
MyString.leftTrim = function(str,start)
ExpandedBlockStart.gifContractedBlock.gif
dot.gif{    
InBlock.gif    
if( str.length>0 && str.indexOf(start)==0 )
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        str 
= str.substring(start.length,str.length);        
InBlock.gif        str 
= this.leftTrim(str,start);
InBlock.gif        
ExpandedSubBlockEnd.gif    }
    
InBlock.gif    
return str;
ExpandedBlockEnd.gif}

None.gif
None.gifMyString.rightTrim 
= function(str,end)
ExpandedBlockStart.gifContractedBlock.gif
dot.gif{    
InBlock.gif    
if( str.length>0 && str.lastIndexOf(end) == str.length-end.length )
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        str 
= str.substring(0,str.length-end.length);        
InBlock.gif        str 
= this.rightTrim(str,end);
ExpandedSubBlockEnd.gif    }

InBlock.gif    
return str;
ExpandedBlockEnd.gif}

None.gif
//end class MyString
None.gif
    MyString.prototype = new String();

None.gif
None.gif
function VR3D()
ExpandedBlockStart.gifContractedBlock.gif
dot.gif{
InBlock.gif    
//this.VR3D() = function(){}
InBlock.gif
    this.Name;
InBlock.gif    
this.ChangeName = function(name)
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
this.Name = name;
ExpandedSubBlockEnd.gif    }

InBlock.gif    
this.Url;
InBlock.gif    
this.SetUrl = function(url)
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
this.Url = url;
ExpandedSubBlockEnd.gif    }

InBlock.gif    
this.GetUrl = function()
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
if(this.Url)
InBlock.gif            
return this.Url;
InBlock.gif        
else
InBlock.gif            
return "";
ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}

None.gif

转载于:https://www.cnblogs.com/wannaCNBLOGS/archive/2006/04/11/372144.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值