📂 JAVASCRIPT/그외
[ 비공개 ] defineGetter 🕔 2017. 7. 4. 15:09
<script type="text/javascript"> ( function () { function defineGetter ( objTarget, strName, funcGetter ) { var getter = { enumerable: true , configurable: true , writable: true , get: funcGetter }; if ( Object.defineProperty ) { try { Object.defineProperty( objTarget, strName, getter ); } catch ( error ) { delete getter.writable; try { Object.defineProperty( objTarget, strName, getter ); } catch ( e ) { objGetter.enumerable = false; Object.defineProperty( objTarget, strName, getter ); } } } else if ( objTarget.__defineGetter__ ) { objTarget.__defineGetter__( strName, funcGetter ); } else objTarget[ strName ] = funcGetter; } var stringProto = String.prototype; var getter = function () { return this.substr( 0, 1 ); }; defineGetter( stringProto, "firstLetter", getter ); }()); </script>
<button onclick="testing()"> Click me </button> <p id="demo"></p> <script type="text/javascript"> function testing () { var string = "Welcome my blog! ; demo.innerHTML = string.firstLetter; } </script>
'JAVASCRIPT > 그외' 카테고리의 다른 글
[ 자바스크립트 ] typeof VS Object.prototype.toString (0) | 2017.12.07 |
---|---|
[ 자바스크립트 ] window의 속성 목록 (1) | 2017.04.30 |
[ 자바스크립트 ] document의 속성 목록 (0) | 2017.04.28 |
[ 미완성 ] getElementsByClassName (0) | 2017.02.28 |
[ 자바스크립트 ] classList (0) | 2016.09.27 |