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
= 1; //Error - //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
= new AClass(); - 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::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