Multiple Left-Hand Assignment with JavaScript

Multiple left-hand assignment with JavaScript

Actually,

var var1 = 1, var2 = 1, var3 = 1;

is not equivalent to:

var var1 = var2 = var3 = 1;

The difference is in scoping:

function good() {  var var1 = 1, var2 = 1, var3 = 1;}
function bad() { var var1 = var2 = var3 = 1;}
good();console.log(window.var2); // undefined
bad();console.log(window.var2); // 1. Aggh!

Multiple left-hand assignment with JavaScript, really right associative?

tl;dr — JS works out where to put the value before working out what that value is, and a side effect of working out what that value is changes the value of a.


See the spec for simple assignment.

Step 1 is "Let lref be the result of evaluating LeftHandSideExpression."

Step 2 is "Let rref be the result of evaluating AssignmentExpression."

So the first thing that happens is that a property x is created on the object stored in a (where n is 1).

Then the right hand side is evaluated (which ends up overwriting a with a new object where n is 2).

Then the result of that expression (that object where n is 2) is assigned to x on the original object (where n is 1).

You can see this in effect with:

"use strict";var a = {n: 1};var b = a;
a.x = a = {n: 2};
console.log(a);console.log(b);

Why expression as left-hand side in assignment doesn't always work?

The relevant bit of the specification is here:

It is a Syntax Error if AssignmentTargetType of LeftHandSideExpression is not simple.

and here.

The simplified version is that (if we leave destructuring aside) you can assign a value to a variable or to a property of an object.

Given (foo), the parenthesis are pointless and the expression is just a variable name. This is fine.

Given (foo1 && foo2 && foo3) the values are read from the variables, compared using the && operator and the outcome is a value.

You can't assign a value to a value, only to a property or a variable.

How to assign multiple variables at once in JavaScript?

In ES6 you can do it this way:

var [a, b] = ["one", "two"];

The above code is ES6 notation and is called array destructuring/object destructuring (if it's an object).

You provide the array on the right-hand side of the expression and you have comma-separated variables surrounded by square brackets on the left-hand side.

The first variable maps to the first array value and so on.

Why can't I use left hand assignment to modify value of function result?

You're pretty much asking for variable variables, i.e. resolving variables for assignment by name, and that is only possible in very very limited capacity in Javascript. The only time you can use it is when assigning an object property where the property name is dynamic:

foo[bar] = baz;

Functions return values, not references nor variables, so assigning to the return value of a function is a complete non-starter.

You're somewhat on the right track with this:

(reference) => reference.split('.').reduce((o,i)=>o[i], window)

It resolves a dotted name to a value, but again, you cannot assign to that resolved value. When a function does return alert, it returns the value alert refers to, which is something like function () { ... }. So what you're trying to do is equivalent to:

function () { ... } = new Proxy(...)

And that just doesn't work. You cannot return a "name", you can only return the value the name refers to.

Your function would have to return the parent object of the one you're looking for, and you can then assign to a property of it, e.g.:

ref(foo)[bar] = baz;

This would be equivalent to, e.g.:

window['alert'] = new Proxy(...)

I.e., setting a property alert on the object that window refers to. And that obviously works just fine.

Since the above use of the ref function is somewhat awkward, the best you can do it wrap that into a function like this:

assign(foo, baz)

That could look something like this:

function assign(name, obj) {
const path = name.split('.');
const parent = path.slice(0, -1).reduce((o, i) => o[i], window);
parent[path[path.length - 1]] = obj;
}

const name = 'foo.bar.baz';
assign(name, new Proxy(ref(name), { ... }));

Note that the object in question must be available as a property on window though (or some other root object that you could pass into assign); there's no (sane) way to assign to a let or const or any local function variable in this manner.

E.g. this could work:

let foo = { bar: {} };
assign('bar', new Proxy(...), foo);

This just ain't happening:

let foo = {};
assign('foo', new Proxy(...));

How does this left hand side assignment for a function call work in JScript?

It is a JScript way to access lists and not an actual function. Therefore it does not throw an Invalid left-hand side in assignment.

StringData is a list of values. What you are actually setting is the value of the 0 index. It is like setting the value to an array using arr[0] = 'xyz'. In your example you could also omit it:

record.StringData = "CustomAction:: " + msg;

Syntax

propVal = Record.StringData
Record.StringData = propVal

Property value

Required field number of the value within the record, 1-based.

Remarks

The returned value of a nonexistent field is an empty string. To set a record string field to null, use either an empty variant or an empty string. Attempting to store a value in a nonexistent field causes an error.

Source: Patrick

Invalid left-hand side in assignment expression in solidjs

You can't use ?. operator with =

Do this instead

if (document.querySelector('.App')) {
document.querySelector('.App').style.backgroundColor = "#fff"
}

Uncaught SyntaxError: Invalid left-hand side in assignment: I am not sure what is causing this error?

Because the employee is a class instead of an object, I think you have to call the constructor.

So this should be what the employees would look like

const employees = [
new Employee(id,"Paul","Jakens","Senior Salesmen", 13000,"2015/05/15")
...
]

Also you can remove the id from the constructor since you're randomizing it

constructor(
name,
surname,
jobtitle,
salary,
startDate
){
this.id = Math.floor(Math.random() * 1000) + (new Date()).getTime();;
this.name = name;
this.surname = surname;
this.jobtitle = jobtitle;
this.salary = salary;
this.startDate = startDate;
}


Related Topics



Leave a reply



Submit