Giter Site home page Giter Site logo

tiretbas-natives's Introduction

tiretbas-natives

Let's extend the builtins safely!

Historically,

extending builtins has been considered a bad practice if not harmful. If not done carefully, it can also be future-hostile.

The future hostility was rooted in the fact that extensions were using names that may end up being chosen by standards (and it happened indeed, more often that not). What web developers need to extend built-ins safely is a namespace that is guaranteed to never be used by ECMAScript or WHATWG standards.

Experience has shown that standards never add built-ins prefixed with _. The only exceptions are de facto standards for __proto__ and __defineGetter__ & friends. Beyond this observation, ECMAScript 6 will have symbols, a way to extend the platform without collisions. Symbols are even used by the ES6 spec for meta-programming extensions like @@create and @@iterator. In the end, the _ prefix looks safe to use.

New builtins

Based on Symbols

Array.prototype

last : returns the last element of the array

import {last} from '<path>/extend-builtin.js'

const myArray = [87, 45, 9, 34];

console.log(myArray[last]); // 34

EventTarget.prototype

on: addEventListener alias

off: removeEventListener alias

once: like addEventListener but removing itself right after the event happened. This is particularly useful for the DOMContentLoaded event for which a good practice is to remove the listener to avoid leaking memory.

emit: dispatchEvent alias

Expectations and licence

I expect to be credited for this work

It is CC0-1.0 licenced

tiretbas-natives's People

Contributors

davidbruant avatar

Stargazers

 avatar

Watchers

 avatar  avatar  avatar

tiretbas-natives's Issues

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.