Skip to content

Consider defining p5 methods as non-writable properties on window (in global mode) #1317

@toolness

Description

@toolness

I was just looking at #1314, where a user accidentally overrwrote p5's text function, and realized that one potential way to prevent this from happening is by defining p5's methods (in global mode) to be non-writable properties of the window object. Here's an example:

Object.defineProperty(window, 'text', {
   value: function() {
     console.log("TEXT WAS CALLED");
   },
  writable: false
});

text += "blah";

text();

In the above example, the assignment to text will silently fail, and the subsequent calling of text() will succeed.

Two alternatives that I think are possible:

  1. Throw a helpful error if the value is overwritten, instead of failing silently.
  2. Permit overwriting the value, but log a helpful warning telling the user that they just overwrote a core p5 method.

I guess alternative (2) might be best if we don't want to break backwards compatibility... Or we can just leave the current behavior as-is, of course.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions