1. 程式人生 > >構造函數 基本使用

構造函數 基本使用

UNC b2b com hello none 構造 selector new replace

相關知識點:

構造函數、原型對象、實例對象

技術分享圖片
<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <title>Document</title>
  <style>
    * {
      margin: 
0; padding: 0; } .wrap { margin: 50px; width: 200px; height: 200px; border: 1px solid red; } .active { border-color: yellow; } </style> </head> <body> <div class="wrap" data-html="hello"></div> <script> function
$(s) { return document.querySelector(s); } function attr(obj, data) { return obj.getAttribute(data); } function ChangeEle(obj) { this.ele = obj; this.ele.dataHtml = attr(this.ele, ‘data-html‘); } ChangeEle.prototype.myHtml = function () {
var ele=this.ele; ele.innerHTML = ele.dataHtml; } ChangeEle.prototype.replaceStr = function (str) { var ele=this.ele; ele.innerHTML = ele.innerHTML === ele.dataHtml ? str : ele.dataHtml; } ChangeEle.prototype.addEvent = function (type, fn) { this.ele.addEventListener(type, fn); } var myWrap = new ChangeEle($(‘.wrap‘)); myWrap.myHtml(); myWrap.addEvent(‘click‘, function () { myWrap.replaceStr(‘world‘); }); </script> </body> </html>
View Code

構造函數 基本使用