npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

enhance-prototype

v1.0.2

Published

Enhance the prototype of Class.

Downloads

8

Readme

enhance-prototype

用于对类的prototype进行加强。

原则

遇到冲突优先权给原生类。


安装

npm install enhance-prototype --save


例子

  • 创建控制器
var {createEnhanceInProto} = require("enhance-prototype")
let ctrl=createEnhanceInProto(Array)
  • 添加自定义方法
ctrl.addProp('sum',()=>{
  this.reduce((acc,cur)=>acc+cur,0)
})
  • 调用自定义方法
let arr=[1,2]
arr.sum() // 3

API

  • createEnhanceInProto

    注入prototype的加强方式:

                 enhanceProto
     .__proto__       |
                  prototype         
     .__proto__       |
               entity(rawClass)

    当执行自定义方法时,会先查找原生的方法,找不到才去查找enhanceProto(相对更安全,尽量不去修改原生类的方法和属性)。

    所有原生创建的实例原型链都会受到改变。

  • createEnhanceOutofProto

    非注入prototype的加强方式:

      ```
                 rawClass.prototype         
       .__proto__       |
                   enhanceProto
       .__proto__       |
                   enhanceEntity
      ```

    只有通过createEnhanceOutofProto控制器创建才是加强实例,原生类创建的实例不受任何影响, 可以通过toRawtoEnhance对加强实例和原生实例互相转换。

    当执行自定义方法时,会先查找enhanceProto,找不到再去原生类的原型链查找。

    非注入prototype的特点是加强类的方法和原生类是互不影响的,你可以在加强类自定义方法上任意调用原生类的方法, 进行包装。

  • controller

  1. addProp(name,value)

    添加自定义属性,返回controller

    value如果是Functionfunction([controller],...args)

    例如:

     let ctrl=createEnhanceInProto(Array)
     ctrl.addProp('immutablePush',function(){
         let newArr=this.slice()
         newArr.push(...arguments)
         return newArr
     })
     
     let arr=[1,2,3]
     let newArr=arr.push(4)
     // arr:[1,2,3]
     // newArr:[1,2,3,4]

    如果需要使用controller,放在第一个参数位置,会查找传入的形参判断是否需要controller。

    let ctrl=createEnhanceInProto(Array)
    ctrl.addProp('showController',function(controller){
       console.log(typeof controller.addProp==="function")    
       return controller
    })
    let arr=[1]
    arr.showController()
        
    // true
    // {addProp: ƒ, removeProp: ƒ, …}
  2. removeProp(name)

    删除自定义方法,返回controller

    填入参数则删除原型链上对应的属性,不填则删除所有属性。

  3. customPropList

    返回当前所有自定义属性,返回当前所有自定义属性<Object>

  4. unMount

    retrieve

    只出现在createEnhanceInProto

    注销当前控制器及自定义prototype及恢复

    removeProp()不同的是,这个会彻底取消原型链,并且清空controller的所有方法,并且额外添加一个retrieve方法。

    可以用来恢复,这时如果设置controller=null将彻底注销。

  5. addBefore(originalName,extraFunc,context)

    addAfter(originalName,extraFunc,context)

    只出现在createEnhanceInProto

    对原生类进行修改(尽量不要)

    参数:

    originalName指要修改的方法名,例如Arraypush方法

    extraFunc指额外添加的方法

    context指上下文,默认为调用push的实例

    例如: ctrl.addBefore('push',()=>console.log('will push'))