This question already has answers here:
Self-references in object literals / initializers
(30 answers)
Closed 5 years ago.
I am declaring:
var ENV = {
VERSION: 1,
SERVER: 'midomain.com',
SERVER_DEV: 'testdomain.dev',
API_ROUTE: 'socket/',
API: (false) ? this.SERVER + '/' + this.API_ROUTE : this.SERVER_DEV + '/' + this.API_ROUTE };
should get:
{ VERSION: 1,SERVER: 'midomain.com',SERVER_DEV: 'testdomain.dev',API_ROUTE: 'socket/', API: 'testdomain.dev/socket/' }
but I get
{ VERSION: 1,SERVER: 'midomain.com',SERVER_DEV: 'testdomain.dev',API_ROUTE: 'socket/', API: 'undefined/undefined' }
Because in that case context is global, not an your object. You can check it with an example:
window.b = 666;
let a = {
prop: this.b
}
console.log(a); // display: {prop: 666}
You can fill your object properties one-by-one
let ENV = {};
ENV.VERSION = 1;
ENV.SERVER = 'midomain.com';
ENV.API = ENV.SERVER;
or create function with 'new' syntax
var ENV = new function() {
this.VERSION = 1;
this.SERVER = 'midomain.com';
this.API = this.SERVER;
};
Related
This question already has answers here:
How to execute a JavaScript function when I have its name as a string
(36 answers)
Closed 2 years ago.
This is what I have attempted, and may give a better gist of the question I'm trying to ask:
var x = "run";
var y = "Function";
var xy = x + y;
function runFunction() {
console.log("Function has been called.");
}
xy();
What am I doing wrong here?
You could use eval(), but don't. Instead, store your functions in an object:
const functions = {
greetingOne: () => console.log("Hello!"),
anotherGreeting: () => console.log("Hi there!");
};
const f = "greetingOne";
functions[f]();
It is possible if the function lives on an object.
const obj = {
runFunction: function() {console.log('hello')}
}
var x = "run";
var y = "Function";
var xy = x + y;
obj[xy]();
You can call eval that run string as Javascript code
function runFunction() {
console.log("Function has been called.");
}
functionName = 'runFunction'
eval(functionName + '()');
All global functions stores in window object.
let first = "first";
let second = "Second";
let xy = first+second;
function firstSecond() {
return "Hello World";
}
console.log(window[xy]());
This question already has answers here:
delete or override const variables in javascript Harmony / ECMAScript 6
(2 answers)
Closed 3 years ago.
I was trying to figure this out with a friend.
Let's say you have a function like this:
const foo = function(i) { return function(j){ return 2 * j } };
How would I override foo with a different method?
Let's say I want to reuse foo with a different anonymous function that does something like function(j){ return 3 * j; };
How would I do that?
EDIT: I saw this post, but that's for variables. I'm asking specifically about anonymous methods.
EDIT 2: This is a proof of concept, so let's say that foo is a const and I can't turn it into a let
const is used for constants, so it's not for overriding.
If you want to override your variable, use let or var
const constantFunction = x => x + 1;
let variableFunction = x => x + 2;
try {
variableFunction = x => x + 3;
console.log('Re-assigning to a variable: DONE');
} catch(e) {
console.log('Error while re-assigning to a variable');
}
try {
constantFunction = x => x + 3;
console.log('Re-assigning to a constant: DONE');
} catch(e) {
console.log('Error while re-assigning to a constant');
}
This question already has answers here:
Use of 'prototype' vs. 'this' in JavaScript?
(15 answers)
What's the difference between this.function and prototype.function?
(1 answer)
Closed 4 years ago.
What is the difference between these two methods of declaring an object, from which you can make an instance? Are there pros and cons to each?
//Method 1:
var myThing1 = function(){
this.a= 2,
this.m = function() {
return this.a + 1;
}
}
var myThing1Instance = new myThing1();
//Method 2:
var myThing2 = {
a: 2,
m: function() {
return this.a + 1;
}
};
var myThing2Instance = Object.create(myThing2)
This question already has answers here:
ES6 arrow functions not working on the prototype?
(4 answers)
Are 'Arrow Functions' and 'Functions' equivalent / interchangeable?
(4 answers)
Closed 5 years ago.
TypeError: this.initializeSieve is not a function
at PrimeGenerator.generatePrimeNumbers. I use constructor mode and prototype mode to create a class and I just call other functions declared in PrimeGenerator.prototype and it report error, did I call them wrong?
function PrimeGenerator() {
this.s = 0;
this.f = [];
this.primes = [];
}
PrimeGenerator.prototype = {
constructor: PrimeGenerator,
generatePrimeNumbers: maxValue => {
if (maxValue < 2) {
return []; // 对不合理值返回空数组
} else {
this.initializeSieve(maxValue); // TypeError happens here!
// this.sieve();
// this.loadPrimes();
return this.primes;
}
},
initializeSieve: maxValue => {
this.s = maxValue + 1;
this.f = [];
let i;
// 将数组初始化为true
for (i = 0; i < this.s; i++) {
this.f[i] = true;
}
// 去掉已知的非素数
this.f[0] = this.f[1] = false;
}
};
const primeGenerator = new PrimeGenerator();
const centArr = primeGenerator.generatePrimeNumbers(100);
console.log(centArr);
This question already has answers here:
Javascript: Do I need to put this.var for every variable in an object?
(6 answers)
Closed 7 years ago.
Why isn't this code working? I'm trying to use the method to add up the properties and then assign the added up properties to its own value.
function howLongILivedSomewhere(college, home1, home2) {
this.birthHome = 18;
this.college = college;
this.home1 = home1;
this.home2 = home2;
this.calcYearsAlive = function() {
return birthHome + college + home1 +home2;
};
this.yearsAlive = calcYearsAlive();
}
var me = new howLongILivedSomewhere(4, 2, 3);
console.log(me);
You missed this keyword during your method / property call. Try like below.
function howLongILivedSomewhere(college, home1, home2) {
this.birthHome = 18;
this.college = college;
this.home1 = home1;
this.home2 = home2;
this.calcYearsAlive = function() {
return this.birthHome + this.college + this.home1 + this.home2;
};
this.yearsAlive = this.calcYearsAlive();
}
var me = new howLongILivedSomewhere(4, 2, 3);
console.log(me.yearsAlive); // output: 27
What is this keyword?
A function's this keyword behaves a little differently in JavaScript compared to other languages. It also has some differences between strict mode and non-strict mode.
Reference:
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/this
How does "this" keyword work within a function?
Update the constructor to:
function howLongILivedSomewhere(college, home1, home2) {
this.birthHome = 18;
this.college = college;
this.home1 = home1;
this.home2 = home2;
this.calcYearsAlive = function() {
return this.birthHome + this.college + this.home1 + this.home2;
};
this.yearsAlive = this.calcYearsAlive();
}
var me = new howLongILivedSomewhere(4, 2, 3);
console.log(me);
Use this keyword when you need to access the object properties (see here more details).