Jump to content

How many variables can I have


cve60069

Recommended Posts

Hello

 

I am trying to establish the number of variables that I can manipulate using a single javascript object and I attach a module. So far, I have passed seven. Are there more I could access if need, particularly within the input object?

 

variables.html

 

Regards

 

Link to comment
Share on other sites

There's no limit to the amount of properties an object can have, or the amount of elements an array can have, but you're confusing properties with HTML attributes. in HTML, there's a fixed number of attributes an element is allowed to have based on its DTD.

 

The alt or data attributes don't exist for the <form> and <input> elements, so there's no Javascript property assigned to them. It also means your HTML isn't valid. If you want to access the value of a non-standard attribute, use getAttribute().

var M = element.getAttribute("data");var N = element.getAttribute("alt");
Link to comment
Share on other sites

You shouldn't be creating attributes because that invalidates your HTML document. However, HTML 5 allows you to create special attributes that should be considered valid by prefixing them with the keyword "data-"

Here's an article about that: http://ejohn.org/blog/html-5-data-attributes/

Access the data using getAttribute() for best compatibility.

 

However, there probably are better ways to do what you want without changing the HTML of your page, but you'll have to explain your ultimate goal.

Link to comment
Share on other sites

Let's try this approach:

function cool_object()   {    this.id=Math.random();    this.created= new Date();    console.log("It's Alive!");   }//Now Let's try it outa= new cool_object();b= new cool_object();

You can see that instead of adding attributes to the Html markup, you can create an object with javascript with just about anything you want.

 

Prototype is used to add methods to javascript objects, and is also needed for extending 'classes' you create with JS. (they aren't exactly classes after all...)

 

Believe me when I say that the above approach using this.property_name to assign properties will give you enough to work with for years.

 

However,

 

Maybe you want to say "It's Alive"- using the above example- by explicitely calling a method name from the cool_object class.

function cool_object()   {    this.id=Math.random();    this.created= new Date();   }cool_object.prototype.talk= function()   {    alert("It's Alive!");   }//Now Let's try it outa= new cool_object();a.talk();

And that's it in a nutshell.

 

Notice how the method is defined outside of the original cool_object function.

 

This is because javascript is not a 'true' OOP language, but it CAN behave just like one.

 

Hopefully you can glean a few good things from this lesson. You can always refer to MDN for more info on OOP javascript. You will see that my examples are very similar to the ones provided there. That's because that's all there is to it!

Edited by Dreadful_Code
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
×
×
  • Create New...