0

I'm new to js world and find jquery declares many properties as methods which make me very uncomfortable.Such as $("#foo").parent() which i think should be a property.

I know that js can also define property so i want to try redefine these method to corresponding property.

Object.defineProperty($.fn,"parent",
        {
            get:function () {
                return this.parent()
            },
            configurable:false,
            enumerable:true
        });

then i can use it like this $("#foo").parent

but i got a stackoverflow

jqueryplus.js:180 Uncaught RangeError: Maximum call stack size exceeded
    at n.fn.init.get [as parent] (jqueryplus.js:180)
    at n.fn.init.get [as parent] (jqueryplus.js:181)
    at n.fn.init.get [as parent] (jqueryplus.js:181)
    at n.fn.init.get [as parent] (jqueryplus.js:181)
    at n.fn.init.get [as parent] (jqueryplus.js:181)
    at n.fn.init.get [as parent] (jqueryplus.js:181)
    at n.fn.init.get [as parent] (jqueryplus.js:181)
    at n.fn.init.get [as parent] (jqueryplus.js:181)
    at n.fn.init.get [as parent] (jqueryplus.js:181)
    at n.fn.init.get [as parent] (jqueryplus.js:181)

What's happening here? In my mind, it should be possible for variables/property and methods to have the same name which i'm familiar with in other language such as c#,kotlin...

4
  • 2
    Since you are new, you should embrace the new JS idioms instead of trying to change them. Notice that the jQuery parent method takes an optional parameter, which your getter would not be capable of. Commented Nov 6, 2017 at 14:55
  • 3
    "it should be possible for variables/property and methods to have the same name" - nope, that's not how JavaScript works. They live in the same "namespace" - and methods are really just normal properties which have a function object as their value. That's why your getter tries to get itself. Commented Nov 6, 2017 at 14:56
  • I'd suggest you learn to deal with properties as methods. This is a rather large waste of time which is just going to create more problems than it solves. Commented Nov 6, 2017 at 15:01
  • thanks for your advices Commented Nov 6, 2017 at 15:08

2 Answers 2

1

You're defining Object.parent to call himself, that's an infinite recursive loop.

The reason why jQuery defines functions to get properties is that it computes dynamic values at runtime, using an oldish coding style (getters and setters exist only from ES5.1 specs). maybe what you could do is create shortcuts for those properties (Object.prototype.parinstead of parent), but there's no effort on doing that IMHO.

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

Comments

0

here this.parent() is make calling function recursively. so give some other name to ur defined function something like getparent Object.defineProperty($.fn,"getparent", call this as $("#foo").getparent

Comments

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.