1

Context

I have a piece of code that runs through karma with jasmine and phantomjs as a browser, and my problem is that I can't figure out why the delete keyword does not do its job. I looked on phantomjs github to find a hint or a clear documentation about keywords support, in vain.

UPDATE

It actually seems it does not work on chrome either now...

here is the output of the expectation

ShopDataServiceTest should not have an instance method remove FAILED Expected { getModelName : Function } not to have method 'remove'.

Here is my tested code:

// CRUDService basically returns a new class
// with a prototype containing a method remove

ShopDataService = CRUDService.build(modelName);
delete ShopDataService.prototype.remove;

Here the code that is executed by karma

expect(ShopDataService.prototype).not.toHaveMethod('remove');

Important note

This code is running under karma and does not work in that case, but it works when running on chrome, am I missing something here ?


EDIT I did not mention it before, the expression typeof ShopDataService.prototype.remove returns 'function', as ppoliani pointed out.

2 Answers 2

1

Well that's the awkward moment when you realize you prototyped wrong !

Why I was wrong

ShopDataService simply does not have its own property remove, since it is inherited from CRUD which has itself a method remove in its prototype.

I had this

var CRUD = function CRUD(modelName) {
        this.getModelName = function () {
            return modelName;
        };
    };

    CRUD.prototype = {
        save: function () {
            // ABSTRACT
        },
        /**
         * Deletes instance from id property
         * @return http promise
         */
        remove: function () {
            // call api
        }
    };

And now it works with this

var CRUD = function CRUD(modelName) {
        this.getModelName = function () {
            return modelName;
        };

        this.save = function () {};
        this.remove = function () {};
    };

As a side effect, I should not hit the CRUD prototype to define the remove method, but instead define it on the newly created prototype extending CRUD.

0

If you're running your code in 'strict mode'; it might not allow you to use the delete operator to full extent.

10
  • Well I get your point, I have strict mode enabled indeed. But the thing is I did not use any descriptor to partially or fully freeze my prototype. That may also explain why disabling strict mode did not solve my problem...
    – axelduch
    Feb 18, 2014 at 16:26
  • When you say it doesn't work in Karma, you mean the assertion is false?
    – ppoliani
    Feb 18, 2014 at 16:31
  • Yes, the assertion is false, no problem with the code itself.
    – axelduch
    Feb 18, 2014 at 16:32
  • can you check if the delete removes the method, indeed; Try something like this var deleted = typeof ShopDataService.prototype.remove !== 'function';
    – ppoliani
    Feb 18, 2014 at 16:39
  • I tried earlier and it turns out it is still a function
    – axelduch
    Feb 18, 2014 at 16:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.