电脑网站拦截怎么解除,冕宁住房和建设局网站,众包网站建设,企业官网的意义Function 在 中是一个很特殊的对象#xff0c;其特殊性体现在它的多重身份。 Function 可以声明普通的函数#xff0c;这一点和其他语言中的函数概念是相同的。除此以外#xff0c;Function还可以用作类型的声明和实现、对象的构造函数#xff0c;以及类引用。 Apply和Call… Function 在 中是一个很特殊的对象其特殊性体现在它的多重身份。 Function 可以声明普通的函数这一点和其他语言中的函数概念是相同的。除此以外Function还可以用作类型的声明和实现、对象的构造函数以及类引用。 Apply和Call方法可以将函数绑定到其它对象上执行。 使用for(…in…)语句可以遍历对象的所有属性和方法。如下面的例子就遍历了test1对象的属性和方法如果是属性刚输出属性如果是方法刚执行方法。 function test1() { this.p1p1; this.p2p2; this.f1function() { alert(f1); } this.f2function() { alert(f2); } } var obj1new test1(); //遍历t的所有属性 for(p in t) { //如果是函数类型刚执行该函数 if(typeof(t[p])function) t[p](); //输出其它类型的属性 else alert(t[p]); } 1. 类的声明 1、使用this关键字 function test1() { this.p1p1; this.p2p2; this.f1function() { alert(f1); } this.f2function() { alert(f2); } } 在中成员变量没有私有与公有机制但可以通过在类内部使用var来声明局部变量。其作用域是类定义的内部。 2、使用ptototype方式的类声明 如果要声明一个类的实例属性或方法可以使用中的对象的prototype属性。例如 Test1.prototype.prop2”prop2”; Test1.prototype.method2function(){ Alert(this.prop2); } 使用prototype属性声明类如下 function test() { } test.prototype{ p1:p1; p2:p2; f1 : funciton () { alert(f2); } } 2. 继承 本身并没有提供继承的语法支持但是在仍然可以采用复制属性和对象的方法实现继承。 function test2(){} for(p in test.prototype) { test2.prototype[p]test.prototype[p]; } test2.prototype.newMethodfunction() { alert(newMethod); } 另外可以参考Prototype框架实现继承的方法 object.extendfunction (destination,source) { for(property in source) { destination[property]source[property]; } return destiantion; } function test2() {} test2.prototypeobject.extend ({newMethod :function () {alert (“newMethod”);} }, Test.prototype ); 3. 多态 多态的实现可以采用和继承类似的方法。首定义一个抽象类其中可以调 用一些虚方法虚方法在抽象类中没有定义而是通过其具体实现类来实现的。例如 object.extendfunction (destination,source) { for(property in source) { destination[property]source[property]; } return destiantion; } //定义一个抽象基类 function base() {} base.prototype{ initialize:function(){ this.oninit();//调用了一个虚方法 } } function test() { //构造函数 } test.prototypeobject.extend( { prop:prop, oninit :function() { alert(this.prop) } },base.prototype ); function test2() { } test2.prototypeobject.extend( { prop2: prop2; oninit:function() { alert(this.prop2); } },base.prototype } 转载于:https://www.cnblogs.com/Frontview/archive/2008/11/07/1328631.html