Jump to content
Search Community

Object.prototype error with tweenmax

yopsolo test
Moderator Tag

Warning: Please note

This thread was started before GSAP 3 was released. Some information, especially the syntax, may be out of date for GSAP 3. Please see the GSAP 3 migration guide and release notes for more information about how to update the code to GSAP 3's syntax. 

Recommended Posts

trigger this in chrome

Uncaught TypeError: Cannot read property 'length' of undefined TweenMax.min.js:16R._updateRoot TweenMax.min.js:16r.dispatchEvent TweenMax.min.js:16

 

<html>

 
<head>
<script>
 
Object.prototype.test = function()
{}
 
</script>
</head>
 
<body>
</body>
 
<html>

See the Pen vEEWrW by yopsolo (@yopsolo) on CodePen

Link to comment
Share on other sites

  • 2 years later...

You can't just say, "That's considered a very poor practice" because it's not. The prototype is a very powerful tool when used correctly.  It might be poor practice to not check to see if the Object.prototype already has a function named test.

 

I ran into the same issue. We add several Object.prototype utilities for working with objects and TweenMax throws a heck of a fit with VERSION: 1.19.1

 

That is very frustrating that TweenMax will error out an entire system when you mess with the prototype of Object. That should be fixed instead of telling people not to do it.

Link to comment
Share on other sites

This error seems to originate on line 5487 (well in the Chrome developer tools) where TweenMax calls any and all functions on the object, regardless if it's on the prototype chain or not. This should check if object hasOwnProperty and not call functions on the prototype chain.

 

In the meantime, if you want to alter the Object.prototype you can define the property like so:

/**
 * Create a shallow copy of an object while breaking all references.
 * Slow, but effective
 * let t = {a:0}
 * let s = t.copy();
 */
if(typeof Object.prototype.copy === 'undefined'){
  Object.defineProperty(Object.prototype, 'copy', {
    enumerable: false,
    get: function(){
      return ()=>{
        return JSON.parse(JSON.stringify(this));
      }
    }
  })
}

 

Screen Shot 2017-08-01 at 8.44.30 AM.jpg

Link to comment
Share on other sites

Isn't there a saying like, "Don't modify objects you don't own"?

 

If extending Object.prototype is causing issues with GSAP, it's likely to cause issues with other libraries too, so wouldn't it be better to make sure your stuff isn't enumerable? 

 

 

Link to comment
Share on other sites

39 minutes ago, allebrum said:

You can't just say, "That's considered a very poor practice" because it's not.

 

Sorry, but I strongly disagree. I have read that sentiment in many places and it makes a lot of sense to me (that messing with the native Object.prototype is dangerous). We place a HUGE priority on speed/performance in GSAP, and implementing your suggestion would slow things down for everyone. It's a painful tradeoff. See what I mean? 

Link to comment
Share on other sites

I'm not here to argue about best practices. I was irritated that I landed here looking for an answer and the final response on the thread was more or less "it's bad, don't do it; nevermind that tweenmax errors out."

 

I appreciate that GSAP places a huge priority on speed, which is why it's the only third-party javascript library that we use client-side.

 

I didn't appreciate that someone's "best-practices" got in the way of providing a meaningful response to someone who seemed like they were trying to learn.

Link to comment
Share on other sites

Cool, thanks for the feedback. And it means a lot to us that GSAP is the only 3rd party library you use client-side. 

 

In terms of providing a meaningful response to someone who seemed like they were trying to learn, do you mind summarizing how our answer could have been better? Are you saying you had hoped that we'd provide more explanation about the "why" behind it being considered a poor practice so that the user could understand? I originally tried to help by asking why he was altering Object.prototype so that perhaps we could offer a better approach, but he never responded to that question so I wasn't sure how to best address it but I guess it would have been better to offer more of the "why" initially. 

Link to comment
Share on other sites

I did see that you had asked why they were wanting to do it, which provides that you were willing to help further. I think that's great.

 

First, I realize you guys answer a lot of questions, and on a whim, I probably would have responded in the same fashion.

 

When I say meaningful, I mean answering their "assumed" question (since they didn't ask a real question), which is "Why isn't this code working, or how do I make this work."  Telling them not to do it, or that it's not good practice, doesn't tell them why TweenMax is erroring out with their code. Even though the issue is very unrelated, it seems at first glance to be an issue with TweenMax.

 

I realize it's not your job to troubleshoot their code, but explaining that "TweenMax loops through all the properties on a target object (which in this case inherits `test` from Object.prototype) and if it's a function, calls that function. What you would need to do is make `test` non-enumerable on the Object.prototype so TweenMax does not error out." 

 

From here, you could choose to explain what you see as best practices.

 

That's my humble opinion. 

 

Cheers!

 

 

 

 

  • Like 3
Link to comment
Share on other sites

51 minutes ago, allebrum said:

I realize it's not your job to troubleshoot their code, ..

 

Hello @allebrum and Thanks for your feedback. ;)

 

You will find throughout this community and throughout this forum. That you will find 1000's of instances of the GreenSock team and the great GreenSock community members troubleshooting other peoples code free of charge. Even when its not related to the GSAP API. With some members troubleshooting others code lasting hours if not sometimes days with little or no appreciation, or sleep.

 

You should also notice in these forums, that a lot of people in the community are willing to help, even when they are treated poorly like on StackOverflow. But we thrive on being polite and wish everyone the best to learn in a productive positive environment .

 

Happy Tweening :)

  • Like 5
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...