js类成员笔记
/**//*
Patterns模式
Public公共
function Constructor()
{
this.membername = value;
}
Constructor.prototype.membername = value;
Private私有
function Constructor()
{
var self = this;
var membername = value;
function membername() {}
}
Note: The function statement
function membername() {}
is shorthand for
var membername = function membername() {};
Privileged特权 一个特权A privileged 方法可以访问私有的变量和方法,同时它对公共域可见。也可以删除或替换一个特权方法,但不能改变它。
特权方法是用 this 在构造器中分配的
function Constructor()
{
this.membername = function ()
{
self.membername;
};
}
Static
Constructor.Method = function()
{
this.member;
}
Add public Method;
这个技巧一般用来添加公共方法。当一个成员被检索且没有在对象中发现的时候,那么它就会从对象构造器的 prototype 成员中去获取它。
这个原型机制可用来实现继承。它也保存内存。要为一个构造器生成的所有对象加入一个方法,将函数加入构造器的prototype 中:
Container.prototype.myMethod = function ()
{
this.member;
}
*/
//class MyString
function MyString(strs)
{
var _str = strs;//private field;
this.Str = _str;//public field
//public method
this.leftTrim = function(start)
{
ret = leftTrim(this.Str,start); //this=new MyString(strs);
return ret;
}
//private method
function leftTrim(str,start)
{
if( str.length>0 && str.indexOf(start)==0 )
{
str = str.substring(start.length,str.length);
str = leftTrim(str,start);
}
return str;
}
this.rightTrim = function(end)
{
ret = rightTrim(_str,end);
return ret;
}
function rightTrim(str,end)
{
if( str.length>0 && str.lastIndexOf(end) == str.length-end.length )
{
str = str.substring(0,str.length-end.length);
str = rightTrim(str,end);
}
return str;
}
var self = this;
this.Trim = function()
{
str = rightTrim(self.leftTrim(" ")," ");
return str;
}
}
//static method
MyString.leftTrim = function(str,start)
{
if( str.length>0 && str.indexOf(start)==0 )
{
str = str.substring(start.length,str.length);
str = this.leftTrim(str,start);
}
return str;
}
MyString.rightTrim = function(str,end)
{
if( str.length>0 && str.lastIndexOf(end) == str.length-end.length )
{
str = str.substring(0,str.length-end.length);
str = this.rightTrim(str,end);
}
return str;
}
//end class MyString
MyString.prototype = new String();
function VR3D()
{
//this.VR3D() = function(){}
this.Name;
this.ChangeName = function(name)
{
this.Name = name;
}
this.Url;
this.SetUrl = function(url)
{
this.Url = url;
}
this.GetUrl = function()
{
if(this.Url)
return this.Url;
else
return "";
}
}