Javascript: hiding prototype methods in for loop?

You can achieve desired outcome from the other end by making the prototype methods not enumerable: Object.defineProperty(Array.prototype, “containsKey”, { enumerable: false, value: function(obj) { for(var key in this) if (key == obj) return true; return false; } }); This usually works better if you have control over method definitions, and in particular if you have … Read more

What are the benefits of making properties non-enumerable?

I think the main benefit is to be able to control what shows up when enumerating an object’s properties, such as for in or Object.keys(). MDN explains it well with Object.defineProperty: https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/Object/defineProperty So normally, when people want to add a method to Object, such as a polyfill for some method not supported in old browsers, … Read more

Why is it impossible to change constructor function from prototype?

You cannot change a constructor by reassigning to prototype.constructor What is happening is that Rabbit.prototype.constructor is a pointer to the original constructor (function Rabbit(){…}), so that users of the ‘class’ can detect the constructor from an instance. Therefore, when you try to do: Rabbit.prototype.constructor = function Rabbit() { this.jumps = “no”; }; You’re only going … Read more

Why does String.prototype log it’s object like a standard object, while Array.prototype logs it’s object like a standard array?

Because in a method call the this argument is always (in sloppy mode) casted to an object. What you see is a String object, which was produced from the “test” primitive string value. The array on which you call your method is already an object, so nothing happens and you just get the array as … Read more

What does the function declaration “sub function($$)” mean?

It is a function with a prototype that takes two scalar arguments. There are strong arguments for not actually using Perl prototypes in general – as noted in the comments below. The strongest argument is probably: Far More Than Everything You’ve Ever Wanted to Know about Prototypes in Perl There’s a discussion on StackOverflow from … Read more

Traits in javascript [closed]

function Trait (methods) { this.traits = [methods]; }; Trait.prototype = { constructor: Trait , uses: function (trait) { this.traits = this.traits.concat (trait.traits); return this; } , useBy: function (obj) { for (var i = 0; i < this.traits.length; ++i) { var methods = this.traits [i]; for (var prop in methods) { if (methods.hasOwnProperty (prop)) { … Read more

How to access object prototype in javascript?

var f = function(); var instance = new f(); If you know name of instance class function, you can simply access prototype as: var prototype = f.prototype; prototype.someMember = someValue; If you don’t: 1) var prototype = Object.getPrototypeOf(instance); prototype.someMember = someValue; 2) or var prototype = instance.__proto__; prototype.someMember = someValue; 3) or var prototype = … Read more