Javascript prototype and Dom prototype

本文探讨了在JavaScript中向DOM对象原型添加新方法的技术细节,包括如何为HTMLDocument和Element对象添加getElementsByClassName方法,并解释了这种方法对于提高网页开发效率的作用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 

image

 

1. Once if A has many instances, adding a new method A.B() to A.prototype will make all instances can use B() right away.

2. A.B() will hide A.prototype.B().

3. All Javascript predefined Objects, prototype is not modifiable.

4. All non-javascript  predefined objects, prototype can be defined.

 

 

nsdom.gif

 

 

Dom prototype is more a interface.

    • Interface objects generally do not have "constructor" functionality (cannot create a new DOM instance by using the JavaScriptnew operator). Exceptions include a few interface objects shipped in previous versions of Internet Explorer:

      • Option (alias for HTMLOptionElement)
      • Image (alias for HTMLImageElement)
      • XMLHttpRequest
      • XDomainRequest, which is new to Internet Explorer 8
    • The "prototype" property of interface objects may not be replaced (changed). An interface object's prototype property cannot be assigned a different interface prototype object at run-time.

  • Interface prototype objects

    • Interface prototype objects define the properties available to all DOM instances, but these built-in properties cannot be permanently replaced (e.g., the JavaScript delete operator will not remove built-in properties).

  • Partial DOM Hierarchy Supported By Internet Explorer 8.

 

 

function _MS_HTML5_getElementsByClassName(classList)
{
  var tokens = classList.split(" ");
  // Pre-fill the list with the results of the first token search.
  var staticNodeList = this.querySelectorAll("." + tokens[0]);
  // Start the iterator at 1 because the first match is already collected.
  for (var i = 1; i < tokens.length; i++)
  {
    // Search for each token independently
    var tempList = this.querySelectorAll("." + tokens[i]);
    // Collects the "keepers" between loop iterations
    var resultList = new Array();
    for (var finalIter = 0; finalIter < staticNodeList.length; finalIter++)
    {
      var found = false;
      for (var tempIter = 0; tempIter < tempList.length; tempIter++)
      {
        if (staticNodeList[finalIter] == tempList[tempIter])
        {
          found = true;
          break; // Early termination if found
        }
      }
      if (found)
      {
        // This element was in both lists, it should be perpetuated
        // into the next round of token checking...
        resultList.push(staticNodeList[finalIter]);
      }
    }
    staticNodeList = resultList; // Copy the AND results for the next token
  }
  return staticNodeList;
}
HTMLDocument.prototype.getElementsByClassName = _MS_HTML5_getElementsByClassName;
Element.prototype.getElementsByClassName = _MS_HTML5_getElementsByClassName;

转载于:https://www.cnblogs.com/ylu20/archive/2010/10/31/1865475.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值