How to call function after ".", javascript [duplicate] - javascript

This question already has answers here:
Add method to string class
(6 answers)
Closed 3 years ago.
I have this sample function, and i want to call it in this format :
const myfunc = (string) => {
return string.length
}
console.log("check_length".myfunc())
How can i do that?
Thanks for answers in advance!

The only way is to mutate the prototype of String with a classic function for accessing this.
String.prototype.myfunc = function () {
return this.length;
};
console.log("check_length".myfunc());

Related

Get values of property in a class [duplicate]

This question already has answers here:
Loop through an array in JavaScript
(46 answers)
Why is using "for...in" for array iteration a bad idea?
(28 answers)
Closed 2 years ago.
I created a class "basketContent" with two properties (idCamera and lenses) which each have values. I can get several instance of this class. Now I would like to get the value of all the idCamera of each instance. I tried with this, but it's not working for now.
basketContent = localStorage.getItem("basketContent");
console.log(basketContent);
for(idCamera in basketContent){
let itemCamera = cameras.find(cameras => cameras['_id'] == idCamera);
console.log(itemCamera);
}
try this
basketContent = JSON.parse(localStorage.getItem("basketContent")) || {};
console.log(basketContent);
for(idCamera in basketContent){
let itemCamera = cameras.find(cameras => cameras['_id'] == idCamera);
console.log(itemCamera);
}

Retrieving the variable value to act as a name [duplicate]

This question already has answers here:
How to use a variable for a key in a JavaScript object literal?
(16 answers)
Closed 5 years ago.
I need an object in Typescript declared like this:
queryParameters = { flagged: true };
Now I would like to have the flagged to be retrieved from a variable name. Something like:
var param = 'flagged';
queryParameters = { ValueOf(param): true };
Any idea ?
Thanks.
Why not use computed property names:
queryParameters = { [param]: true };

Function which refers to an object. Both are in an object. What does "this" mean? [duplicate]

This question already has answers here:
How does "this" keyword work within a function?
(7 answers)
How does the "this" keyword work, and when should it be used?
(22 answers)
Closed 6 years ago.
Nope, the title is not an enigma.
I have :
obj = {
NaNException:function(message=''){
this.message=message;
},
input:{
integer:function(a){
var b = prompt(a);
if (isNaN(parseInt(b, 10)))
throw new this.NaNException();
else
return b;
}
}
}
so, in NaNException:function(){ }, this is expected to be NaNException, but in integer, it is expected to be obj... which interpretation is the good one? And what can I do with the wrong one to have the expected result ?
Thanks in advance.

How to access sub-values of a multi-dimensional object [duplicate]

This question already has answers here:
Convert a JavaScript string in dot notation into an object reference
(34 answers)
Closed 7 years ago.
is it possible to get a reference to an object with the object itself
obj
and the attributes in string form
'address.town.street'
so that at the end it resolves
obj.address.town.street
i could immageine smth like the eval() function.
Try
function getValue(obj, path) {
return path.split(".").reduce(function(obj, name){ return obj[name]}, obj);
}
Do not use eval. Use this instead
Object.prototype.nestedByString=function(reference){
var current=this;
path=reference.split(".");
for(var i=0;i<path.length;i++){
current=current[path[i]];
}
return current;
}
Here is a demo
I suppose that if you're allergic to extending native prototypes, you can do this
function nestedByString(obj,reference){
var current=obj;
path=reference.split(".");
for(var i=0;i<path.length;i++){
current=current[path[i]];
}
return current;
}

Difference between object.prop and object["prop"] [duplicate]

This question already has answers here:
Accessing Properties in object [duplicate]
(3 answers)
Closed 7 years ago.
I used to believe that there is no difference between them but after seeing this piece of code, all my information about objects in Javascript failed.
var search = function(name) {
for(var x in friends) {
if(friends[x].firstName === name) {
console.log(friends[x]);
return friends[x];
}
}
};
This code works. But
var search = function(name) {
for(var x in friends) {
if(friends.x.firstName === name) {
console.log(friends.x);
return friends.x;
}
}
};
this doesn't.
Thanks for explaining.
friends.x is not the same thing as friends[x], it's the same thing as friends['x'].
When you use friends[x] the value x can be a variable (or any expression), but when you use friends.x the x is a literal name, it won't use the variable x.
As #Guffa already explained, friends.x and friends['x'] are the same, the difference in those approaches is that when you use [] this syntax you can use variables, 'property like this', or reserved words, this is good when you do not know exactly the property you will need.

Categories