Skip to content
View in the app

A better way to browse. Learn more.

Web Designer Forum

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

Javascript encapsulation

Featured Replies

Hello, all!

 

I was trying to move object left-down-up-right and I have succedd without encapsulation, but now I'm trying to use javascript encapsulation and can't get any good result. All the time I get error: object required or object expected at line 14.

 

Someone has a some idea what I'm doing wrong?

 

You can see my code at:

My link

 

p.s.This is not a professional project, only a small test project for learnig javascript, because I'm real beginner.

Edited by Lana9

Someone has a some idea what I'm doing wrong?

You're actually using jsFiddle wrong. If you look in the top left-hand corner you'll see 2 dropdowns. These define the library to bring in and where to put your JavaScript code. At the moment you've got that set to MooTools and "onLoad" so all your code gets wrapped in the following:

 

window.addEvent('load', function() {
   // Your code here
});

This means that any variable you create inside that can't bee see anywhere other than that JavaScript box.

 

To fix that, change the first dropdown to "no wrap (head)" and, since you're not using any libraries, change the second one to "No-Library (pure JS)"

  • Author

You're actually using jsFiddle wrong. If you look in the top left-hand corner you'll see 2 dropdowns. These define the library to bring in and where to put your JavaScript code. At the moment you've got that set to MooTools and "onLoad" so all your code gets wrapped in the following:

 

window.addEvent('load', function() {
   // Your code here
});

This means that any variable you create inside that can't bee see anywhere other than that JavaScript box.

 

To fix that, change the first dropdown to "no wrap (head)" and, since you're not using any libraries, change the second one to "No-Library (pure JS)"

 

Hello Skateside,

thank you for your response. I took your advice and done everything what you said, but I have a bunch of errors, still.

Typeof: "Problem at line 14 character 20: Missing radix parameter.

return parseInt(this.element.offsetLeft);".

Also, I ran my code at PhpED, errors are the same. Of course, can't run code on button click, although /onclick event/ should call function for starting animation.

 

This is how my animation should work(project with no encapsulation):

My first project

Edited by Lana9

Hello Skateside,

thank you for your response. I took your advice and done everything what you said, but I have a bunch of errors, still.

Typeof: "Problem at line 14 character 20: Missing radix parameter.

return parseInt(this.element.offsetLeft);".

Also, I ran my code at PhpED, errors are the same. Of course, can't run code on button click, although /onclick event/ should call function for starting animation.

 

This is how my animation should work(project with no encapsulation):

My first project

The parseInt function is designed to interpret any number (or string that looks like a number) into a real integer. Usually it can successfully guess whether that's supposed to be base 10, 8 or 16, but occassionally it gets it wrong. To stop that, you can use the radix parameter to tell it how it should interpret the number. I'm guessing you want your number to be base 10, so here's how you say so:

 

animWidth = parseInt(hello.offsetWidth, 10);

What other errors are you getting?

  • Author

The parseInt function is designed to interpret any number (or string that looks like a number) into a real integer. Usually it can successfully guess whether that's supposed to be base 10, 8 or 16, but occassionally it gets it wrong. To stop that, you can use the radix parameter to tell it how it should interpret the number. I'm guessing you want your number to be base 10, so here's how you say so:

 

animWidth = parseInt(hello.offsetWidth, 10);

What other errors are you getting?

 

 

Now, I have only this two errors: Problem at line 26 character 34: Use '!==' to compare with 'null'.

if(window.innerWidth != null){

 

Implied global: window 26,31,32.

 

Thank you, a lot for your advices!!! This was really helpfull.

Now, I have only this two errors: Problem at line 26 character 34: Use '!==' to compare with 'null'.

if(window.innerWidth != null){

 

Implied global: window 26,31,32.

 

Thank you, a lot for your advices!!! This was really helpfull.

The problem with != is that is can lie, when checking against things like null it will say it's the same as false or undefined. That's why the adive is to never compare with !=, but instead use !==

 

if (window.innerWidth !== null) {

As for the implied globals, it's tough to see what it could be refering to. Try moving your functions so they're declared before they're called, that might help.

Create an account or sign in to comment

Account

Navigation

Search

Search

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.