´ÓÕâƪÆ𣬻áÓÉdzµ½ÉîµÄ·ÖÎöjs OO֮дÀ෽ʽ£¬´ó¸Å»áÓÐ5-8ƪ¡£ºóÃæ½Ðø»á·ÖÎöÁ÷Ðп⣨¿ò¼Ü£©µÄдÀ෽ʽ¡£ÎªÁËÌÖÂ۵ĵ¥Ò»ÐÔ£¬Ôݲ»¿¼ÂÇÀàµÄ¼Ì³Ð£¬(˽ÓÐ,Êܱ£»¤)ÊôÐÔ»ò·½·¨¡£
EMCA262¹æ·¶ÖÐûÓÐÀà(class)µÄ¸ÅÄjsµÄnewÖ»ÊÇÈÃËû¿´ÆðÀ´¸üÏñc++,javaÒ»µã¡£ÕâÀï˵µÄдÀֻ࣬ÊÇÊéдjs´úÂë·ç¸ñ¶øÒÑ¡£
1¡¢¹¹Ô캯Êý·½Ê½
/**
* PersonÀࣺ¶¨ÒåÒ»¸öÈË£¬ÓиöÊôÐÔname£¬ºÍÒ»¸ögetName·½·¨
* @param {String} name
*/
function Person(name) {
this.name = name;
this.getName = function() {
return this.name;
}
}
ÕâÖÖ·ç¸ñÈÃÎÒÃÇÕâЩд¹ýjavaµÄÓеãÇ×ÇÐÔÚÓÚ¹¹ÔìÒ»¸ö¶ÔÏóÐèÒªÅäÖÃһЩ²ÎÊý£¬²ÎÊýÒª¸³Öµ¸øÀàÀïÃæthis¡£µ«ÓëjavaµÄÇø±ðÊÇjsÓÃfunctionÀ´¶¨ÒåÀ࣬²ÎÊýÒ²ÎÞÐ趨ÒåÀàÐÍ¡£
ÀàдºÃÁË£¬ÎÒÃÇÔ켸¸ö¶ÔÏó£º
var p1 = new Person("Jack");
var p2 = new Person("Tom");
console.log(p1 instanceof Person);//true
console.log(p2 instanceof Person);//true
¿ØÖÆ̨Êä³öÒ²Ö¤Ã÷ÁËp1£¬p2µÄÈ·ÊÇÀàPersonµÄ¶ÔÏóʵÀý¡£
ÕâÖÖ·½Ê½µÄÓŵãÊÇ£º¿ÉÒÔ¸ù¾Ý²ÎÊýÀ´¹¹Ô첻ͬµÄ¶ÔÏóʵÀý
£¬È±µãÊǹ¹Ôìʱÿ¸öʵÀý¶ÔÏ󶼻áÉú³ÉgetName·½·¨°æ±¾£¬Ôì³ÉÁËÄ ......
<script language='javascript'>
function toggleAll(cb)
{
var val = cb.checked;
var frm = document.forms[0];
var len = frm.elements.length;
var i=0;
for( i=0 ; i<len ; i++)
{
if (frm.elements[i].type=="checkbox" && frm.elements[i]!=cb)
{
frm.elements[i].checked=val;
}
}
}
</script>
//Sample Usage
<form >
Check ALL
<input type="checkbox" name="checkall" onclick="toggleAll(this)" id="Checkbox1" />
<br>
<input type="checkbox" name="check1" id="Checkbox2" />
<input type="checkbox" name="check2" id="Checkbox3" /& ......
<html>
<body>
<script type="text/javascript">
//·½·¨Ò»
var num_arr= [];
for (var i = 0 ; i < 10 ; i ++)
{
num_arr[i] = i;
}
document.write(num_arr+'</br>');
//·½·¨¶þ
var obj_arr=new Array();
obj_arr[6]='ss';
document.write(obj_arr.length);
</script>
</body>
</html>
......
Grouping Scripts ³É×é½Å±¾
Since each <script> tag blocks the page from rendering during initial download, it's helpful to limit the total number of <script> tags contained in the page. This applies to both inline scripts as well as those in external files. Every time a <script> tag is encountered during the parsing of an HTML page, there is going to be a delay while the code is executed; minimizing these delays improves the overall performance of the page.
ÓÉÓÚÿ¸ö<script>±êÇ©ÏÂÔØʱ×èÈûÒ³Ãæ½âÎö¹ý³Ì£¬ËùÒÔÏÞÖÆÒ³ÃæµÄ<script>×ÜÊýÒ²¿ÉÒÔ¸ÄÉÆÐÔÄÜ¡£Õâ¸ö¹æÔò¶ÔÄÚÁª½Å±¾ºÍÍⲿ½Å±¾Í¬ÑùÊÊÓá£Ã¿µ±Ò³Ãæ½âÎöÅöµ½Ò»¸ö<script>±êǩʱ£¬½ô½Ó×ÅÓÐÒ»¶Îʱ¼äÓÃÓÚ´úÂëÖ´ÐС£×îС»¯ÕâЩÑÓ³Ùʱ¼ä¿ÉÒÔ¸ÄÉÆÒ³ÃæµÄÕûÌåÐÔÄÜ¡£
The problem is slightly different when dealing with external JavaScript files. Each HTTP request brings with it additional performance overhead, so downloading one single 100 KB file will be faster than downloa ......
Dynamic Script Elements ¶¯Ì¬½Å±¾ÔªËØ
The Document Object Model (DOM) allows you to dynamically create almost any part of an HTML document using JavaScript. At its root, the <script> element isn't any different than any other element on a page: references can be retrieved through the DOM, and they can be moved, removed from the document, and even created. A new <script> element can be created very easily using standard DOM methods:
Îĵµ¶ÔÏóÄ£ÐÍ£¨DOM£©ÔÊÐíÄãʹÓÃJavaScript¶¯Ì¬´´½¨HTMLµÄ¼¸ºõÈ«²¿ÎĵµÄÚÈÝ¡£Æä¸ù±¾ÔÚÓÚ£¬<script>ÔªËØÓëÒ³ÃæÆäËûÔªËØûÓÐʲô²»Í¬£ºÒýÓñäÁ¿¿ÉÒÔͨ¹ýDOM½øÐмìË÷£¬¿ÉÒÔ´ÓÎĵµÖÐÒƶ¯¡¢É¾³ý£¬Ò²¿ÉÒÔ±»´´½¨¡£Ò»¸öеÄ<script>ÔªËØ¿ÉÒԷdz£ÈÝÒ×µØͨ¹ý±ê×¼DOMº¯Êý´´½¨£º
var script = document.createElement_x("script");
script.type = "text/javascript";
script.src = "file1.js";
document.getElementsByTagName("head")[0].appendChild(script);
This new <script> element loads the source file fi ......
XMLHttpRequest Script Injection XHR½Å±¾×¢Èë
Another approach to nonblocking scripts is to retrieve the JavaScript code using an XMLHttpRequest (XHR) object and then inject the script into the page. This technique involves creating an XHR object, downloading the JavaScript file, then injecting the JavaScript code into the page using a dynamic <script> element. Here's a simple example:
ÁíÒ»¸öÒÔ·Ç×èÈû·½Ê½»ñµÃ½Å±¾µÄ·½·¨ÊÇʹÓÃXMLHttpRequest(XHR)¶ÔÏ󽫽ű¾×¢Èëµ½Ò³ÃæÖС£´Ë¼¼ÊõÊ×ÏÈ´´½¨Ò»¸öXHR¶ÔÏó£¬È»ºóÏÂÔØJavaScriptÎļþ£¬½Ó×ÅÓÃÒ»¸ö¶¯Ì¬<script>ÔªËؽ«JavaScript´úÂë×¢ÈëÒ³Ãæ¡£ÏÂÃæÊÇÒ»¸ö¼òµ¥µÄÀý×Ó£º
var xhr = new XMLHttpRequest();
xhr.open("get", "file1.js", true);
xhr.onreadystatechange = function(){
if (xhr.readyState == 4){
if (xhr.status >= 200 && xhr.status < 300 || xhr.status == 304){
var script = document.createElement_x("script");
&n ......