<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <script type=text/javascript charset=utf-8> (function(){ //2中函数声明的区别 add(1,1); function add(x,y){ alert(x+y) } add(1,2); //add2(12,3)//不能调用 var add2 = function(x,y){ alert(x+y) } add2(12,3) //传值还是传址,string是基础类型, var i = 100; var s = "one"; function add3(i,s){ i++; s+="--" } alert(i);//100 or 101 alert(s);//"one" })() var a = 3; var b = [a]; alert(b instanceof Array); alert(b[0]); var a = "[1,2,3,4,5]"; var array = eval(a);//string变数组 for (var i = 0; i < array.length; i++) { alert(array[i]) } //解析成函数,并且调用 var str = "var show = function(){alert(100)}()"; eval(str); new person().showName(); var cat = {}; Object.getPrototypeOf(cat).name = "MAOMI"; cat.__proto__.master = "USPCAT.COM"; var a = {};//空类 a.__proto__ = person.prototype; var b = {}; b.__proto__ = new person(); b.__proto__.constructor = b; var JSON = {}; JSON.prototype = { toJSONString :function(){ var outPut = []; for(key in this){ outPut.push(key+" -- "+this[key]) } return outPut; } } function mixin(receivingClass,givingClass){ for(methodName in givingClass.prototype){ //本类中没有这个函数的情况下我在聚合,否则跳过 receivingClass.prototype[methodName] = givingClass.prototype[methodName] } } var o = function(){ this.name = "YUN"; this.age = 17 } mixin(o,JSON); alert(JSON.prototype.toJSONString); alert(o.prototype.toJSONString); var a = new o(); alert(a.toJSONString()); JSON.prototype['toJSONString'] = function(){ var outPut = []; for(key in this){ outPut.push(key+" ------ "+this[key]) } return outPut; } mixin(o,JSON); alert(JSON.prototype.toJSONString); alert(o.prototype.toJSONString); alert(a.toJSONString()); </script> </head> <body> </body> </html>
/** * 掺元类 * 有的适合只需要继承一个类(几个)中的一些函数 * */ (function(){ //我们准备将要被聚合的函数 var JSON = { toJSONString :function(){ var outPut = []; for(key in this){ outPut.push(key+" --> "+this[key]) } return outPut; } }; /** * 聚合函数 */ function mixin(receivingClass,givingClass){ for(methodName in givingClass){ if(!receivingClass.__proto__[methodName]){ //通过中括号访问json receivingClass.__proto__[methodName] = givingClass[methodName] } } } var o = {name:"YUN",age:27} mixin(o,JSON); document.write(o.toJSONString().join(",")) //------------------------------------------------------------------- JSON.prototype = { toJSONString :function(){ var outPut = []; for(key in this){ outPut.push(key+" --> "+this[key]) } return outPut; } } //制作聚合函数 function mixin(receivingClass,givingClass){ for(methodName in givingClass.prototype){ //本类中没有这个函数的情况下我在聚合,否则跳过 if(!receivingClass.prototype[methodName]){ //传递的是地址 receivingClass.prototype[methodName] = givingClass.prototype[methodName] } } } //---------------------------------------------------------- //var o = {name:"YUN",age:27} var o = function(){ this.name = "YUN"; this.age = 17 } mixin(o,JSON); var a = new o(); document.write(a.toJSONString().join(",")) })()