`

Javascript - 初窥

阅读更多
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <title>chat.html</title>
	
    <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
    <meta http-equiv="description" content="this is my page">
    <meta http-equiv="content-type" content="text/html; charset=UTF-8">
    
    <!--<link rel="stylesheet" type="text/css" href="./styles.css">-->
   	<script type="text/javascript">
    //定义id抽取函数.....目前只能抽取ID
   	var $=function(id){
   		return document.getElementById(id);
   	};
   	//定义javascript对象:java类
   function FuncObj(name,  job, born){
   		this.name = name;
   		this.job = job;
   		this.born = born;
   		function fn2(){
   			alert(this.new);
   		};
   		new_it:"i am new push by FcunObj fn2";
   };
   //在函数原型操作类:java——对类的操作与方法进行修改
   	FuncObj.prototype.fn=function(){
   		alert("fn");
   	};
   	
   	function showColor() {
 		 alert(this.color);
	};
   	
   	function click_(){
   		showColor();
   		//跟Java也太像了吧~
   		new FuncObj().fn();
   		var obj2 = new FuncObj();
   		alert(obj2.fn2);
   		alert(obj2.new_it);
   	//	obj2.fn2();
   		alert(FuncObj.toString());
   		alert(FuncObj.prototype.fn.toString());
   		alert("enter click");
   		alert($("fst_div"));
   	};
   	
    
   	</script>

  </head>
  
  <body>
    <div id="fst_div" ></div>
    <button onclick="click_();">______</button>
    <div></div>
    <div></div>
  </body>
</html>


/**javascript 模拟 StringBuffer();*/

function StringBuffer () {
  this._strings_ = new Array();
}

StringBuffer.prototype.append = function(str) {
  this._strings_.push(str);
};

StringBuffer.prototype.toString = function() {
  return this._strings_.join("");
};
sb.append("123");
sb.append("456");

document.write(sb.toString());

/**测试性能,跟预期结果相反*/
var d1 = new Date();
var str = "";
for (var i=0; i < 10000; i++) {
    str += "text";
}
var d2 = new Date();

document.write("Concatenation with plus: "
 + (d2.getTime() - d1.getTime()) + " milliseconds");

var buffer = new StringBuffer();
d1 = new Date();
for (var i=0; i < 10000; i++) {
    buffer.append("text");
}
var result = buffer.toString();
d2 = new Date();

document.write("<br />Concatenation with StringBuffer: "
 + (d2.getTime() - d1.getTime()) + " milliseconds");
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics