`When you extend a $resource object, it effectively removes the prototype from the

Here's the current code snippet I'm working with:

// Factory
angular.factory('Student', function() {
    var defaults = {
        StudentId: null
    };

    var Student = function(params) {
        angular.extend(this, angular.copy(defaults), params);
    };

    // Prototype function for Student
    Student.prototype.hasStudentId = function() {
        return this.StudentId != null;
    };
});

// Service
angular.service('StudentService', ['Student', '$resource', function(Student, $resource) {
    var service = {};

    service.student = $resource('student/:studentId'. {} { load: {method: 'GET'} });

    service.loadStudent = function(studentId) {
        var currentStudent = service.student.load(studentId); // HTTP call

        currentStudent.$promise.then(function() {
            // When extending currentStudent, it doesn't have the hasStudentId() prototype function
            angular.extend(currentStudent, new Student(currentStudent));

            // However, using 'new' keyword gives studentObj the proper prototype hasStudentId() function
            var studentObj = new Student(currentStudent);
        });
    };

    return service;
}]);

After using Angular.extend on currentStudent, the object does not contain the hasStudentId() prototype function. If I instantiate a new instance with new Student(currentStudent), it works correctly.

Is Angular.extend disregarding the prototype functions?

At the moment, currentStudent only inherits the prototype functions of $resource.

Answer №1

According to the official documentation, the method angular.extend only duplicates properties that are directly defined on the object, not those inherited from its prototype chain. Even though prototype properties may be enumerable, they are not considered "own" properties of the object. More information on this topic can be found in an article by MDN here.

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

The nonexistence of the ID is paradoxical, even though it is present

I've been working on a school project that involves a dropdown box with the id "idSelect." However, I'm encountering an issue where it says that idSelect is not defined when I try to assign the value of the dropdown box to a variable. Even after ...

The issue with the isolated scope in AngularJS directive persists

I've been studying AngularJS and attempted to implement "Directive with Isolated scope", but unfortunately, it's not functioning as expected. Below are the code snippets I have used: HTML: <body ng-app="myApp"> <div ng-init="myProper ...

Encountering a build time issue while incorporating Redis into Next.js

Incorporating Redis into my Next.js project has been beneficial overall. However, during the build process (next build), an error arises when it attempts to connect to Redis, resulting in the following message: Collecting page data ..[ioredis] Unhandled e ...

How can I uniquely combine a code with an existing CSS class and make modifications to it?

I am using ngx-skeleton-loader and I would like to change the color, but I am facing some difficulties. Here is an image that illustrates the issue. When looking at the developer tools, you can see the styles action in the styles action bar. .loader ...

Setting up routes in VueJS and NodeJS Express for API integration: a comprehensive guide

My API routes always return HTML instead of JSON, despite trying numerous solutions that have all failed. Here is my current setup: // app.js const express = require("express"); const app = express(); const history = require("connect-history-api-fallback ...

A dysfunctional JS object

I am grappling with a rather intricate object and have simplified it by removing unnecessary functions. However, I am facing an issue when I try to initialize it and I can't figure out the reason behind it. Safari is throwing this error at me: [Error] ...

Handling onChange events for several typescript <Select> elements

As a non-TS developer, I'm delving into the realm of multiple selects and dropdown menus with Material-UI's select component. Progressing from a basic setup, I successfully implemented a single select but now face a challenge in adding another dr ...

Editing text with ng-model is not functioning

I am facing a challenge in saving the input from a contenteditable element to my JavaScript code. It seems that the ng-model directive is not functioning as expected in this scenario. <div ng-app="Demo" ng-controller="main"> <input ng-model=" ...

JavaScript inserted into debug console by developer

Is there a method to troubleshoot code that has been added through the firefox developer console terminal? For example, I added document.onkeydown = function(event) { // code logic for checking keys pressed } If only I could determine which .js file t ...

Utilize the Angular 1 function again

Imagine you have two sets of items: available items and associated items. I need to create a search function that utilizes different API methods to search each list separately. After obtaining the search results, I must store them in models using the $scop ...

The renderToString function in Material UI's sx property doesn't seem to have

Every time I apply sx to a component that is rendered as a string and then displayed using dangerouslySetInnerHtml, the styles within the sx prop do not work. Here is an example showcasing the issue: Codesandbox: https://codesandbox.io/p/sandbox/wonderfu ...

Next.js Error: The text displayed on the page differs from the HTML rendered by the server

Currently, I am facing an issue with the error "Error: Text content does not match server-rendered HTML." Here is my scenario - I have received a dateTime from an API, for example '2022-12-25T11:22:37.000Z', and I want to display it on a compone ...

My AngularJS module seems to be malfunctioning

After spending a significant amount of time on this, I am really hoping for some assistance. I am currently working on creating a module that will generate a directive and controller for the header section of my AngularJS site. Despite not encountering any ...

Utilize Angular Js to play a hidden sound in the background without any visible controls

Is there a method to incorporate a background sound without visible controls using Angular JS? I am currently working on an app using Cordova Visual Studio tools. ...

Using Javascript to delete an HTML list

Currently, I am working on a webpage that includes notifications along with options to mark them as read individually or all at once. However, when attempting to use my loop function to mark all notifications as read, I noticed an issue. let markAllAsRead ...

The issue of Angular's ng-repeat view not refreshing after a search query remains unresolved

Having some trouble with updating a results array in AngularJS using a service call. After submitting a form and calling the service, I set up my callbacks with .then() on the promise object. However, the view only updates when I start deleting characters ...

Click the button to save numerous images at once

We have added two mask images to the page. https://i.sstatic.net/uAbb7.png When a user clicks on a mask image, a file upload dialog box appears, allowing the user to upload their own image and click save. https://i.sstatic.net/V4kFA.png After clicking ...

The function 'myfunc' was called within a render, however, it is not defined in the instance

I keep seeing this error message Property 'myfunc' was accessed during render but is not defined on instance, and I'm not sure why. Below are my HTML and JavaScript code snippets. const ListRenderingApp = { data() { return { todo ...

Monitor the collection for changes before adding an item to the collection

When using ui-select multiple, I am facing an issue where I need to check the collection before ng-model="collection" is updated in order to ensure that the new value is not already present in it. Simply watching the collection does not solve this problem ...

Why does the removeChild method in JavaScript consistently remove the last child element, rather than the one specified by its ID?

There are four divs with an event listener onclick, triggering a JavaScript function that simply removes the clicked div: this.parentNode.removeChild(this); Although it should remove the specific div clicked on, it instead deletes the last child and alte ...