Is it safe to rewrite custom javascript function names for custom objects?

Is it safe when creating a custom object to give it methods (in my current case, "read", "write" and "save") that overwrite js native functions?

The object in question will never have to write to the DOM (or otherwise use the functions (s) that it will play); these method names are just perfect, so I was curious, and then I was surprised when it was difficult for me to find a clear answer to this question. An example is below. Thank you

/**
 * Ticket class
 * @param category
 * @param issuedBy
 * @param reissuable If true, lock cannot be overridden by the same method that locked it
 * @returns {Ticket}
 * @constructor
 */
Ticket = function (category, issuedBy, reissuable) {
    //properties
    this.id = Date.now().toString();
    this.category = category;
    this.resolved = false;
    this.issuingMethod = issuedBy;
    this.reissuable = reissuable === true;
    this.data = {};

    //methods
    this.resolve = function () { return this.resolved = true;};
    this.read = function (dataPath) { // find dataPath in this.data }
    this.write = function (dataPath, value) { // add value to dataPath of this.data}

    return this;
};
+4
source share
2 answers

. new, this .

var ticket = Ticket(); //this.document will point to the global document object

, this, , ;

  • , window (<script>alert(this.name)</script>)
  • (alert()), this window
  • (ticket.resolve()), this (ticket).
  • (new Ticket), this - Ticket.prototype .
  • HTML- (onclick="alert(this.id)"), this HTML,
  • , setTimeout, setInterval AJAX, this window
  • apply call, , this
  • Function.bind, ,

var ticket = Ticket() , this.name = 'something', , name .

, , .

    Ticket = function (category, issuedBy, reissuable) {
    if (!this instanceof Ticket) { // called as a function
         return new Ticket(category, issusedBy, reissuable);
    }
    //properties
    this.id = Date.now().toString();

, http://js-bits.blogspot.com/2010/08/constructors-without-using-new.html

, , , ,

+2

- Object.prototype. , , Object.prototype, .

function MyObject() {}

MyObject.prototype.hasOwnProperty = function () { return true; };

new MyObject().hasOwnProperty('test'); //true

, , , Object.prototype.hasOwnProperty.

, new . , , . ( ), , JSHint, .

+2

Source: https://habr.com/ru/post/1540446/


All Articles