11/17


#1

var me = new Object(me.name = "Clara", me.age = 15);

This is my code and I have looked everywhere and I have no clue what is wrong please help!


#2

Why do you have a paranthesis with the properties inside them @claradavies? This is the syntax for creating objects using the dot notation...

var me = new Object(); 
me.name = "Clara";
me.age = 15

#3

Thanks for the answer @amanuel2, I thought the answer had to all be on one line.


#4

Dot notation is a method of retrieving data from associative arrays, not of creating objects.

You can use literal notation to create an object and still use dot notation to retrieve values from it.

Don't mix terminology.


#5

Your right i messud up thats not the normal javascript..

As said from StackOverflow here:

Basically you can do this:

function addValueToObj(obj, newProp) {
    newProp = newProp.split("=");       // separate the "path" from the "value"

    var path = newProp[0].split("."),     // separate each step in the "path"
        val = newProp.slice(1).join("="); // allow for "=" in "value"

    for (var i = 0, tmp = obj; i < path.length - 1; i++) {
       tmp = tmp[path[i]] = {};     // loop through each part of the path adding to obj
    }
    tmp[path[i]] = val;             // at the end of the chain add the value in
}

var x = {a:1, b:2};
addValueToObj(x, "c.d=3");
// x is now {"a":1,"b":2,"c":{"d":"3"}}

#6

Ah, I see. I personally still prefer a dog notation. Basically, you can do this:

function createNewObjectUsingDogNotation(obj) {
    var data = obj.split(" woof "),
        result = {};
    for(var i = 0; i < data.length; i += 2) {
        result[data[i]] = data[i + 1];
    }
    return result;
}

var x = createNewObjectUsingDogNotation("name woof Azor woof age woof 4");

console.log(x);
// >> { name: 'Azor', age: '4' }

Seriously speaking, this is not a matter of normal and abnormal JavaScript. The code you posted is an example of data representation and parsing. Objects are created in these lines:

tmp = tmp[path[i]] = {};
var x = {a:1, b:2};

and as you see script uses literal notation. Not dot notation, not dog notation.


There is nothing wrong with making mistakes (and admitting it). You are doing a great job helping others. keep it up :slightly_smiling:


#7

Thanks but you should probably fix this :smiley:


#8

Why? This is how it's called - dog notation.

name woof Azor woof age woof 4

#9

oh i thought you meant to say dot notation.