How do I change a class variable in Javascript?

PROBLEM SOLVED.

Special thanks to Andrew Noyce for explaining this not with a class but with Object Literal Notation. What's a noob, I ..

EDIT:

Sorry I made a mistake in my js file, my js structure looks like this:

(I am completely new to javascript, I need your guys, help here ...)

var className = {

settings : {
    setA  : true,
    setB: true,
    setC: true,
    },

    Default : {
        move: true,
        remove: true,
        edit: true,\
    },
    ...
}

      

And in my html, index.html, I include the class.js file, but I want to change the setting like:

    Default : {
    move  : false,
    remove: false,
    edit: false,
    } 

      

because only edit.html, users are allowed to move, delete and edit.

How can I do it? Do I have to have 2 js, class_disable.js (include in index.html) and class_enable.js (for edit.html)?

You need some advice ..

I tried:

 className.settings.Default = {move = false, remove = false, edit = false};

      

But it won't work.

+1


a source to share


3 answers


This is invalid syntax:

className.settings.Default = {move = false, remove = false, edit = false};

      

Object values ​​are assigned using colons as shown below:

className.settings.Default = {
    move: false,
    remove: false,
    edit: false
};

      



Alternatively, you can use this syntax:

className.settings.Default.move = false;
className.settings.Default.remove = false;
className.settings.Default.edit: false;

      

What you are using here is not a class, it is called Object Literal Notation and is very common in JavaScript. There is no class or constructor, you just assign properties to the object using literals, thus denoting the literature of the object name.

+3


a source


className.settings = {move: false, remove: false, edit: false};

      



+2


a source


or the long way:

className.settings.move = false;
className.settings.remove = false;
className.settings.edit = false;

      

Another way, if you are using jQuery:

$.extend(className.settings, {move: false, remove: false, edit: false});

      

+2


a source







All Articles