1

is it possible to make something like this ?

let foo = {}, bar = {name : "carl"};
foo.isNotEmpty && "foo is not empty" // FALSE
bar.isNotEmpty && "bar is not empty"; // TRUE


Object.prototype.isNotEmpty = function (){
 return Object.keys(_argument_here).length == 0;
}

I don't know how to use object (in this case foo and bar) in my prototype method. Also i want to use it like property not a function, otherwise it looks like this

foo.isNotEmpty() && "foo is not empty"

I prefer first way but if its not possible second way is okay too. Thanks for your help.

0

2 Answers 2

5

I don't know how to use object (in this case foo and bar) in my prototype method.

You'd use this within the function.

But:

  1. Never, ever, ever add enumerable properties to Object.prototype (which is what Object.prototype.isNotEmpty = ... does). You'll break a huge amount of code that assumes that for-in on {} won't see any properties to visit / Object.keys({}) will return an empty array. If you really want to do it, use Object.defineProperty and don't set enumerable to true (the default is false, so if you just leave it off, you're good).

  2. Even adding non-enumerable properties to Object.prototype is generally strongly discouraged, because of potential conflicts with expected "own" properties of objects.

  3. You need to add it before you call it. In your code, you're trying to access it before you've added it.

  4. To get the result you want without using (), you need to define the function as a property getter (more on that below).

So you could do this (with a non-getter function):

Object.defineProperty(Object.prototype, "isNotEmpty", {
    value: function (){
        return Object.keys(this).length == 0;
    },
    configurable: true
});

let foo = {}, bar = {name : "carl"};
console.log(foo.isNotEmpty());
console.log(bar.isNotEmpty());

...or if you want a getter:

Object.defineProperty(Object.prototype, "isNotEmpty", {
    get: function (){
        return Object.keys(this).length == 0;
    },
    configurable: true
});

let foo = {}, bar = {name : "carl"};
console.log(foo.isNotEmpty);
console.log(bar.isNotEmpty);

But generally it would be better to have your own function (within a scoping construct [not shown] so you're not defining a global):

function isNotEmpty(obj) {
    return Object.keys(obj).length == 0;
}

let foo = {}, bar = {name : "carl"};
console.log(isNotEmpty(foo));
console.log(isNotEmpty(bar));

Sign up to request clarification or add additional context in comments.

4 Comments

Thanks for your tips, i think it'll be better if i can define a helper function which checks object is empty or not, right ?
@Nezih: Yes. Also note that Object.keys only checks for enumerable, own properties. So if the object you're checking has any non-enumerable own properties, you'll still think it's empty when it isn't.
According to Kanstantsin Kamkou's answer. It works pretty good. What do you think ?
@Nezih: I think that answer is wrong, because generic objects like the ones you're using don't have a length property. (But that's what I meant about a "getter" in my answer above.) I refer you to the two working examples above. Separately: In my earlier comment, I forgot to point you at a function: Object.getOwnPropertyNames. That gives you what Object.keys gives you plus "own" non-enumerable properties, as long as they're strings. (If you have Symbol-named properties, you'd also need Object.getOwnPropertySymbols.)
1
Object.defineProperty(
  Object.prototype, 'isNotEmpty', {
   get: function() {
     return this.length > 0;
   }
  }
);

const a = 'test';
console.log(a.isNotEmpty); // true

As mentioned before, javascript is a strange language, please do not pollute prototypes.

8 Comments

Thats what i'm talking about. Thank you.
Generic objects such as the ones in the OP's question have no length property.
strange comments you have. first of all js has no generic objects. objects in js... are also not objects. polluting prototype assuming to detect the type somehow, for example: _.isString(this) or Array.isArray() and so on.
@KanstantsinKamkou: {} is commonly called a generic object (as opposed to the result of new Date or similar). And again: They don't have a length. If you actually ran the code above with the OP's examples, you'd find it doesn't work. Rather than calling the comment strange and nit-picking terminology, you might actually pay attention to the gist of what it's saying. (And I'm afraid the entire second half of your comment ["polluting" onward] is completely unclear in English.)
Plain objects (maybe that is what you called generic object) are those who has no prototype or the prototype is null. i.e. Object.getPrototypeOf(new Date()) and Object.getPrototypeOf({}). Things you're mentioned I'm not familiar with. Sorry. I don't want to continue this topic. Thank you.
|

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.