Overriding function named params in Javascript? [duplicate] - javascript

This question already has answers here:
Where can I get info on the object parameter syntax for JavaScript functions?
(1 answer)
ES6 Object Destructuring Default Parameters
(1 answer)
Closed 2 years ago.
function lookupRecord2({value1 = "blue", value2 = 7}) {
console.log('theValues');
console.log(value1);
console.log(value2);
}
lookupRecord2( { value1: 'pink', value2: 9 } );
I've read that Javascript doesn't allow for named parameters so I'm confused how the above code works. What is happening here that's allowing me to override the params?
I think what's confusing is that the params are assigned with "=" whereas the object I'm passing uses ":" ...
ADDED:
I know this is a form of destructuring, but I still can't make sense of it b/c it's in the function declaration.

function des({ a = 1, b = 2 } = {}) {
console.log(a);
console.log(b);
}
des();
This is a destructing syntax where we're able to pass default values.
It works everywhere, not just in function declarations.
{ a = 1, b = 2 } = { b:3 }
// assigns 1 to "a" and because "b" already exists, "b" is 3
MORE INFO HERE: can someone explain this seemingly weird assignment `{key=value} = argument`

Related

JavaScript insert a new property to inline object only if condition is met [duplicate]

This question already has answers here:
In JavaScript, how to conditionally add a member to an object?
(29 answers)
Closed 2 years ago.
I'm aware with ES6 JavaScript object you can dynamically declare variables as object keys, using [], for example:
{[2 + 2]: "four!"}
gives the output {"4": "four!"}
the question is, can a similar method be done in order to add an entire property, through variables etc., inline? Meaning, like suppose I have the following test object:
var myObj = {
someProp: 2,
someOtherProp: 3 //only add this prop if a condition is met
}
is there anything I can write in the inline object for someOtherProp, to only have it be added to the object, if a certain condition is met? So for example (pseudocode), something like this
var myObj = {
someProp: 2,
[someBool ? null : "someOtherProp: 3"] //only add this prop if a condition is met
}
would give the ouput (considering someBool is true), like the above, but if someBool is false, it would give me
var myObj = {
someProp: 2
}
??
I'm aware I can simply add a property (or delete one) to (/from) the object later, using the [] indexer, like
someBool && (myObj["someOtherProp"] = 3)
as well as make some kind of helper function for this,
but I was wondering if there was a way to do it using the inline-object notation?
You could spread an object with a conditional operator.
{} is a neutral value.
var myObj = {
someProp: 2,
...(someBool ? {} : { someOtherProp: 3 })
}

Weird javascript code convention with "const" [duplicate]

This question already has answers here:
beginner's: const definition in Redux confusing
(1 answer)
What does curly brackets in the `var { ... } = ...` statements do?
(4 answers)
Closed 5 years ago.
What is this code convention in javascript?
const { navigate } = //whatever
As in, what sense does it make. I saw it in RNs React navigation
https://reactnavigation.org/docs/intro/
It's named destructuring. When you have an object and you want to take only a property of that object, you can get only it by using that convention.
let fullName = {
first: 'John',
last: 'Smith'
}
const { first } = fullName;
You can take a look here for more info's
http://wesbos.com/destructuring-renaming/
It's called destructuring
Example:
var myObject = {a: "what", b: "ever"};
const {a} = myObject;
console.log(a); // will give "what"

Undefined when referencing an object via another object in JavaScript [duplicate]

This question already has answers here:
JavaScript - Why can't I call a variable "name"?
(2 answers)
Closed 5 years ago.
My question is pretty simple. I'm creating two objects. The second object is referencing an object inside the first object.
var me = {
name: {
first: "justin"
}
};
var name = me.name;
console.log(me.name.first); // "justin"
console.log(name.first); // undefined
Why am I getting undefined in my second console log? Shouldn't I get "justin" instead?
You need to use another name. There is a name variable which is global.
var me = {
name: {
first: "justin"
}
};
var anotherName = me.name;
console.log(me.name.first);
console.log(anotherName.first);

javascript: define a variable in a function [duplicate]

This question already has answers here:
Is there a better way to do optional function parameters in JavaScript? [duplicate]
(28 answers)
Closed 8 years ago.
In PHP I can do something like this:
public myFunction($variable=0) {
//Do some stuff
}
In Javascript, the following does not work:
var myFunction = function(variable=0 )
{
//do some stuff...
};
Try this
var myFunction = function(variable) {
variable = variable || 0;
// this variant works for all falsy values "", false, null, undefined...
// if you need check only undefiend, use this variant
// variable = typeof(variable) == 'undefined' ? 0 : variable
};
Default function parameters will be in ES6; ES5 does not allow it.
With ECMAScript 6 you can do this.
Check an example:
function multiply(a, b = 1) {
return a*b;
}
multiply(5); // 5
But this is still experimental and today is only supported by Mozilla Firefox browser.
Check this link do see documentantion:
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/Default_parameters#Browser_compatibility

Access attributes inside of object while in object in JavaScript [duplicate]

This question already has answers here:
Self-references in object literals / initializers
(30 answers)
Closed 8 years ago.
I'm having a bit of an inception issue right now. I'm trying to reference an attribute of an object while creating another attribute of the object.
For example:
var x = {
a1 : "a",
b2 : this.a1,
c : "c"
}
alert(x.a1); // Returns properly
alert(x.b2); // Returns undefined.
If I try to make b2 reference x.b2, it doesn't work either. Can anyone point me in the right direction?
All in all, I'm trying to decide the value of b2 based on the value of a1 without having to take another step out of the object.
Here's a fiddle -- http://jsfiddle.net/fpG9h/
You are most definitely in need of getters and setters. You can define them like this
var obj = {
a1: "a",
get b2() {
return this.a1;
},
set b2(value) {
this.a1 = value;
},
c: "c"
}
console.log(obj.b2); // a
obj.b2 = "bbb";
console.log(obj.b2); // bbb
console.log(obj.a1); // bbb

Categories