Javascript下的伪OOP用法测试
JavaScript代码
- <script type="text/javascript">
- <!–
- // 方法一
- var cellphone = {
- number: 13684042440,
- dial: function () {
- }
- };
- // 方法二
- var cellphone = new Object();
- cellphone.number = 13484042440;
- cellphone.dial = function () {
- alert("Calling "+this.number);
- }
- cellphone.dial();
- // 给已有的类追加方法
- String.prototype.trim = function () {
- var tmp = this.replace(/^\s+/,”);
- tmp = tmp.replace(/\s+$/,”);
- return tmp;
- }
- var str = " sfdfd sfsd ";
- str2=str.trim();
- alert("’"+str+"’");
- alert("’"+str2+"’");
- function Beesn() {
- this.member = new Array(‘Kevin’,‘Vonsk’,‘Edison’,‘Michael’,‘Whl’),
- this.list = function () {
- for(var i=0;i<this.member.length;i++) {
- alert(this.member[i]);
- }
- },
- this.show = function () {
- this.member = this.member.shift();
- alert(this.member);
- //for(i in this.member) {
- // alert(this.member[i]);
- //}
- }
- }
- var our = new Beesn;
- our.show();
- //arguments 对象的用法。
- function ArgTest(a, b, c){
- var i, s = "The ArgTest function expected ";
- var numargs = arguments.length; // 获取被传递参数的数值。
- var expargs = ArgTest.length; // 获取期望参数的数值。
- if (expargs < 2)
- s += expargs + " argument. ";
- else
- s += expargs + " arguments. ";
- if (numargs < 2)
- s += numargs + " was passed.";
- else
- s += numargs + " were passed.";
- s += "\n\n"
- for (i =0 ; i < numargs; i++){ // 获取参数内容。
- s += " Arg " + i + " = " + arguments[i] + "\n";
- }
- return(s); // 返回参数列表。
- }
- alert( ArgTest(1,our,our) );
- // caller demo
- function callerDemo() {
- if (callerDemo.caller) {
- var a= callerDemo.caller.toString();
- alert(a);
- } else {
- alert("this is a top function");
- }
- }
- function handleCaller() {
- callerDemo();
- }
- callerDemo();
- //callee可以打印其本身
- function calleeDemo() {
- alert(arguments.callee);
- }
- //用于验证参数
- function calleeLengthDemo(arg1, arg2) {
- if (arguments.length==arguments.callee.length) {
- window.alert("验证形参和实参长度正确!");
- return;
- } else {
- alert("实参长度:" +arguments.length);
- alert("形参长度: " +arguments.callee.length);
- aert(calleeLengthDemo.length);
- }
- }
- //递归计算
- var sum = function(n){
- if (n <= 0)
- return 1;
- else
- return n + arguments.callee(n – 1);
- }
- calleeLengthDemo(1,2,3);
- var vehicle=Class.create();
- vehicle.prototype.initialize= function(type)
- {
- this.type=type;
- }
- vehicle.prototype.showSelf= function()
- {
- alert("this vehicle is "+ this.type);
- }
- var Class = {
- create: function() {
- returnfunction() {
- this.initialize.apply(this, arguments);
- }
- }
- }
- var moto=new vehicle("Moto");
- moto.showSelf();
- // 测试extend
- Object.extend = function(destination, source) {
- for (property in source) {
- destination[property] = source[property];
- }
- return destination;
- }
- Object.prototype.extend = function(object) {
- return Object.extend.apply(this, [this, object]);
- }
- var obj = new Object();
- obj.extend({
- f1: function () {
- alert(‘f1′);
- },
- f2: function () {
- alert(‘f2′);
- }
- });
- obj.f2();
- String.prototype.isEmail = function () {
- return /^[0-9a-z\-_\.][email protected][0-9a-z\-_\.]+\.[a-z]+$/i.test(this);
- };
- var str = "[email protected]";
- alert( str.isEmail() );
- //alert(/^[0-9a-z\-_\.][email protected][0-9a-z\-_\.]+\.[a-z]+$/i.test(str));
- //–>
- </script>
测试方法:
请一段一段的测试。测试一段时,先把别的行注释。