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

get-parent

v1.1.3

Published

获取某个非null对象、数组或函数的父对象;To obtain a not null objects, arrays, or function of the parent object;

Downloads

11

Readme

get-parent

介绍

获取某个非null对象、数组或函数的父对象; To obtain a not null objects, arrays, or function of the parent object;

通过npm引入使用

  1. npm install get-parent --save
  2. import GPI from "get-parent"; // GPI自己可以定义其它名字
  3. GPI.init(objectName, object); // 需要用哪个对象在给他初始化;
  4. childObject.getParent(); // 获取父对象
  5. object.add(key, value); //对象添加新元素,通过此方法添加的对象、数组、函数仍然可以获取到父对象
  6. Array的push/unshift/splice已经改造,增加元素一样可以监听

例子:

  <script>
  import GPI from "get-parent";

  export default {
    name: 'Test',
    mounted(){
      console.log(GPI);
      // ==========Object============
      var obj = {
        a: {
          hi: '123',
          hello: '234'
        },
        b: new String('bcd'),
        array: [1,2,3, {
          aa: 234
        },['a', {b: 'asd'}]]
      }

      console.log("obj --- ", obj);
      GPI.init('obj', obj);

      let t1 = obj.b;
      let t2 = obj.array[3];
      let t3 = obj.array[4][1];
      console.log("t1 --- ", t1);
      console.log("t2 --- ", t2);
      console.log("t3 --- ", t3);
      console.log("t1.getParent() --- ", t1.getParent());
      console.log("t2.getParent() --- ", t2.getParent());
      console.log("t3.getParent() --- ", t3.getParent());
      obj.add('test', {
        'what': 'wow'
      })
      let ww = obj.test;
      console.log(ww.getParent());

      // ==========Array============

      let array = [1,23,4, {
        asd: {
          fff: 2323
        }
      }]

      GPI.init('array', array);

      console.log(array[3]);
      console.log(array[3].getParent());
    }
  }
  </script>

通过script标签引入使用

  1. 路径根据自己的文件位置定义
  2. let GPI = window.GPI;
  3. GPI.init(objectName, object); // 需要用哪个对象在给他初始化;
  4. childObject.getParent(); // 获取父对象
  5. object.add(key, value); //对象添加新元素,通过此方法添加的对象、数组、函数仍然可以获取到父对象
  6. Array的push/unshift/splice已经改造,增加元素一样可以监听

例子:

  // ==========Object============
  let obj = {
    a: 1,
    b: {
      val : 2,
      c: {
        val: 3
      }
    },
    test(a){
      console.log(a);
    },
    d: [1,2,3,4,{
      e: 123
    }],
    e: null,
    f: new String('adf'),
    g: new Number(123)
  }

  let c = obj.b.c;
  let test = obj.test;
  let d = obj.d;
  let GPI = window.GPI;

  GPI.init("obj", obj);
  console.log(obj, "obj");

  console.log("getParent", d.getParent());

  console.log(obj.b.c);
  console.log(obj.b.c.getParent());

  // ==========Array============

  let array = [1,23,4, {
    asd: {
      fff: 2323
    }
  }]

  GPI.init('array', array);

  console.log(array[3]);
  console.log(array[3].getParent());