Discover the item that offers the inherited characteristic

What is the most effective method to determine the specific parent object of a property when dealing with an object that has multiple prototype parents and a known property name implemented on one of them?

For example:

var a = { x: 'foo' };
var b = {};
var c = { y: 'bar' };
var d = { z: 'baz' };
var e = {};
b.__proto__ = a;
c.__proto__ = b;
d.__proto__ = c;
e.__proto__ = d;
alert(e.y); // 'bar'

If we only have a reference to e, our goal is to identify c as it is the origin of the value assigned to

e.y</code. The number of levels between <code>c
and
e</code can vary.</p>

<p>(To provide context, in reality <code>e
represents an AngularJS $scope, therefore this question also pertains to identifying the parent scope that delivers a particular inherited scope property.)

Answer №1

Here is a simple solution that I came up with:

function findOwnProperty(obj, prop) {
    while (obj) {
        if (obj.hasOwnProperty(prop)) {
            return obj;
        }
        obj = obj.__proto__;
    }
    return null;
}

I'm curious if there are any more efficient methods for achieving this task, or if there's an existing function in a library that can handle it?

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Adjust the class of a div element located close to its ancestor using JavaScript

I'm trying to change the class of the element #sidePanel from .compact to .expanded dynamically in this code snippet: <div id="sidePanel" class="compact"></div> <div id="topbar"> <div id="buttonContainer"> <div ...

Having trouble with this code// Does anyone know how to make the div slide in line with other divs when hovering over it?

After spending countless hours analyzing and trying various solutions, I have come to the realization that I need to seek help with my code. The task at hand is proving to be incredibly frustrating, and I have exhausted all other options before resorting t ...

What is the most efficient way to transfer substantial data from a route to a view in Node.js when using the render method

Currently, I have a routing system set up in my application. Whenever a user navigates to site.com/page, the route triggers a call to an SQL database to retrieve data which is then parsed and returned as JSON. The retrieved data is then passed to the view ...

AngularJS is failing to update the shared service model

Utilizing AngularJS, my application contains two controllers that share a common service. When triggering an event controlled by the portalController function (specifically the setLang() function), I notice that the model of the applicationController does ...

Exploring the dynamic duo of AngularJs, Jboss, and the security

If you were to explore JBoss security framework as a potential solution for enabling JAAS using JBoss 6 and creating the web.xml file to configure JAAS security for protecting a Rest API: <?xml version="1.0" encoding="UTF-8"?> <web-app xmlns: ...

The issue encountered is an org.openqa.selenium.TimeoutException error that occurs while attempting to

I am utilizing chimp.js, which adds fibers magic to webdriver.io for synchronous code execution. Here is the snippet: var c = require('./config'); module.exports = function () { this.When(/^I attempt to log in with incorrect credentials$/, ...

Creating Helper Functions for Modifying Arrays in Javascript Without Creating a New Reference: How Can I Extend the Array Class?

This library is designed for managing user lists using socket.io. I've implemented custom methods in an Array extension class. However, I've encountered an issue with the removeUser method. While logging indicates that the user is successfully ...

Optimal Approach for Implementing a Loading Animation in React JS

While attempting to replicate my local project in Code Sandbox, I stumbled upon an error. My goal in these projects is to display a loader spinner while fetching data from an Express + GraphQL server, but unfortunately, the spinner doesn't hide once t ...

Utilizing a directive to enhance form validation

I am looking to retrieve the validation variables provided by AngularJS for forms. I want to be able to add forms using a directive in my code, but currently I cannot access the $dirty,$error, and $invalid variables that I need. My JS: myApp.directive(&a ...

Loop through your elements seamlessly with the innovative Jquery loop plugin

After working on the code for my jQuery plugin, here is a snippet that has been causing confusion: for (var counter = 1; counter <= 3; counter++) { var btn = $("<a></a>").addClass(class1) .addClass(class2 + cou ...

Error: Undefined variable in JavaScript obfuscation

My website has several small JavaScript files that I currently merge into one file and then minimize using terser. The website functions perfectly with the minimized version. Now, I want to take it a step further and obfuscate the code using JavaScript-Ob ...

The JSONP script encountered an error due to an unexpected colon

I am looking to fetch some game information from Steam using an API, however when I try to use it, I encounter the following error: No 'Access-Control-Allow-Origin' header is present on the requested resource. Additionally, when trying to use J ...

Automatically pre-fill and send hidden form

I'm working on a form and have set up a handler for the submit button like this: $( '#submit_btn' ).click( function( data ){ theForm = document.getElementById( 'realForm' ); theForm.meetingName.value = document.getElement ...

Tips for deactivating pagination indicators on the initial page, final page, as well as on the previous and subsequent pages within Laravel

My goal is to create a pagination disable handler that will effectively disable the first and previous page buttons when I am on the first page, allowing only the next page and last page buttons to be clickable. Similarly, when I am on the last page, the n ...

What is the best way to eliminate the [lang tag] from a URL in Next.js?

I am looking to eliminate either the /en or the /it from the URL without explicitly adding it. i18next seems to be doing it automatically, and I am unsure how to disable this behavior. I simply want it to happen in the background. https://i.stack.imgur.co ...

The focus() function fails to execute properly in Vue for NativeScript when v-for is used

One of the challenges I'm facing involves setting focus on the first textfield when a button is pressed. I found a code snippet in the playground without using v-for and it works perfectly fine. However, as soon as I introduce v-for into the code, e ...

Opt for Object.keys() over .length when dealing with Firebase Objects

How can I update this snippet to use Object.keys()? It currently works well when the comment ids are numbers, but how can it be adapted to work with auto-generated ids from Firebase? (The data structure is provided below) ngOnInit() { this.route.param ...

Encountering an EACCES error in Ubuntu when attempting to use bower for installation due to permission denial

When attempting to install bower, I used the command... sudo npm install -g bower I received the following output... npm http GET https://registry.npmjs.org/bower npm http 304 https://registry.npmjs.org/bower /usr/local/bin/bower -> /usr/local/lib/no ...

Issue with passing incorrect props to child component occurs specifically on pages 2 and beyond within react-table

Implementing a button in each row of a table using react-table to trigger a react-modal is functioning correctly on the initial page. However, when navigating to subsequent pages, an issue arises where the incorrect id prop is being passed into the custom ...

Utilizing AngularJS ngResource: Ensuring Consistent Use of the Catch Function

Each time a server request using any $resource fails, I want to display the same alert message to my users. Currently, my code looks like this: function tryAgain() { alert("try again") } myResource.query().$promise.catch(tryAgain); myResource.update(...) ...