如何使用原型进行自定义方法和对象操作

时间:2012-08-31 02:08:11

标签: javascript prototype

老实说,我正在尝试理解JavaScript原型,但我没有取得多大进展。我不确定如何解释我想要做什么,除了说部分我的最终目标是学习如何遍历DOM类似于jQuery并添加自定义方法来操纵被访问的特定元素。

编辑:以下代码已经更新,以反映我从目前为止收到的答案中学到的概念,并展示那些不符合我要完成的目标。

function A(id) {
    "use strict";
    this.elem = document.getElementById(id);
}
A.prototype.insert = function (text) {
    "use strict";
    this.elem.innerHTML = text;
};

var $A = function (id) {
    "use strict";
    return new A(id);
};

var $B = function (id) {
    "use strict";
    return document.getElementById(id);
};

function init() {
    "use strict";
    $A('para1').insert('text goes here'); //this works
    $A('para1').innerHTML = 'text goes here';  //this does not work
    console.log($A('para1')); //returns the object A from which $A was constructed
    console.log($B('para1')); //returns the dom element... this is what I want

    /*I want to have $A('para1').insert(''); work and $A('para1').innerHTML = '';
      work the same way that $B('para1').innerHTML = ''; works and still be able
      to add additional properties and methods down the road that will be able
      act directly on the DOM element that is contained as $A(id) while also
      being able to use the properties and methods already available within
      JavaScript*/
}
window.onload = init;

如果可能的话,请添加一个解释,说明为什么您的代码有效,以及为什么您认为这是实现此目的的最佳方法。

注意:我的调查的全部目的是自己学习这个...请不要建议使用jQuery,它会失败的目的。

3 个答案:

答案 0 :(得分:1)

试试这个。

var getDOM= function(id) {
  this.element= document.getElementById(id);
}

getDOM.prototype.insert= function(content) {
  this.element.innerHTML= content;
}

var $= function(id) {
  return new getDOM(id);
};

$('id').insert('Hello World!'); // can now insert 'Hello World!' into document.getElementById('id')

答案 1 :(得分:1)

var $ = function(id) {  
    return new My_jquery(id);  
}  

function My_jquery(id) { 
    this.elem = document.getElementById(id);
}

My_jquery.prototype = {
   insert : function(text) { this.elem.innerHtml = text; return this;}
}

$('para1').insert('hello world').insert('chaining works too');  

在My_jquery.prototype

中添加您想要对elem进行操作的任何方法

答案 2 :(得分:1)

您可以使用以下方案:

function $(id) {
  return new DOMNode(id);
}

function DOMNode(id) {
  this.element = document.getElementById(id);
}

DOMNode.prototype.insert = function(value) {

  if (value) {

    // If value is a string, assume its markup
    if (typeof value == 'string') {
      this.element.innerHTML = value;

    // Otherwise assume it's an object
    } else {

      // If it's a DOM object
      if (typeof value.nodeName == 'string') {
        this.element.appendChild(value);

      // If it's a DOMNode object
      } else if (this.constructor == DOMNode) {
        this.element.appendChild(value.element);
      }
    }
  } // If all fails, do nothing
}

$('id').insert('foo bar');

一些玩法:

<div id="d0">d0</div>
<div id="d1">d1</div>
<div id="d2">d2</div>

<script>
// insert (replace content with) string, may or may not be HTML
$('d0').insert('<b>foo bar</b>');

// insert DOMNode object
$('d0').insert($('d1'));

// Insert DOM element
$('d0').insert(document.getElementById('d2'));

</script>

您可能会发现研究MyLibrary如何运作很有用,它有一些非常好的做法和模式。

相关问题