Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p><s>Simply use <code>settings.closeButton.text</code></s></p> <p>JavaScript properties can be accessed using one of two methods:</p> <ul> <li><p><strong>Dot notation</strong></p> <p>The most common and basic way of accessing properties - however illegal variable names (except for reserved words - they are allowed as property names under ES5) will not work.</p> <pre><code>foo.bar; // OK foo.class; // only in ES5 and up foo.&amp;^&amp;%^&amp;@&amp;(@&amp;&amp;@; // SyntaxError: yeah, it doesn't work </code></pre></li> <li><p><strong>Square bracket notation</strong></p> <p>When using the square bracket notation, it can take anything - however it will be converted to a string (all object properties in JavaScript are strings):</p> <pre><code>// both are the same foo['bar']; foo["bar"]; // this is fine foo['&amp;^&amp;%^&amp;@&amp;(@&amp;&amp;@']; // this is equivalent to foo["[object Object]"] foo[{}]; </code></pre></li> </ul> <p>Pick your fancy - but unless you need to, it's most likely easier to use dot notation to access JavaScript object properties.</p> <p><strong>EDIT:</strong> about your jsFiddle, here's why it doesn't work:</p> <pre><code>var options = { // Passing these options in msg: 'This is my message', closeButton: { text: "Close this", colour: "red" } }, // These are the defaults if none are passed in settings = $.extend({ title: 'Default Title', msg: 'Default message', closeButton: { text: "Close", colour: "red", btnClass: "pull-right" } }, options); console.log(settings.closeButton.text); console.log(settings.closeButton.colour); console.log(settings.closeButton.btnClass); /* settings.closeButton.text settings.closeButton.colour settings.closeButton.btnClass */ </code></pre> <p>When you're calling <code>$.extend()</code>, any properties in the later arguments will replace those in the earlier ones. In this case, your <code>closeButton</code> property in your <code>$.extend()</code> call is being replaced by the one in <code>options</code>, since the arguments was given later.</p> <p>Here's an example of this in action:</p> <pre><code>var a = { foo: 'bar' }; var b = { foo: 'baz' }; var c = $.extend(a, b); var d = $.extend(b, a); console.log(c.foo); // baz (b was last argument) console.log(d.foo); // bar (a was given last) </code></pre> <p>To solve this issue, either swap the arguments, or (in this case acceptable) perform a deep copy, by prepending the arguments with <code>true</code>:</p> <pre><code>$.extend({ a: { b: 1, c: 2 } }, { a: { b: 3 } }).a; // { b: 3 } $.extend(true, { a: { b: 1, c: 2 } }, { a: { b: 3 } }).a; // { b: 3, c: 2 } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload