In JavaScript, how to execute the next function from an array of functions

I have a series of functions, such as: funcArray = [func1, func2, func3]; Within a given function, I want to execute the next function in the array. How do I do this? This is my basic skeleton: function func1() { // I get current function caller var currentFunc = func1.caller; // I want to execute the next function. Happens to be func2 in the example. } I cannot use the indexOf function, as with arrays of strings or numbers. Note: This question appears to be similar to this one and what it refers to. However, this is a different question. I want to change the order of processing by modifying the array. This is the goal. There may be a more efficient method. Clarification: According to some comments: funcArray is global. The goal is to implement middleware for the Node.js HTTP module in as simple and effective way as possible without using any third-party modules. 1> T.J. Crowder..: You can’t reach for func2< unless func1 ends funcArray /code> and execute it, you shouldn’t either. Even if func1 does close funcArray, there is no separation of concerns func1 comes out in order to find its own funcArray and then execute func2.…

In JavaScript, why can’t the constant String be modified, but the constant array can be modified?

Because the array itself doesn’t change, it’s still the same array. In C-like languages, something similar to this is a constant pointer (to some address in memory) – you can’t change where the pointer points, But the same content can be written to the memory. In Javascript, this pointer-like behavior applies to anything that is not primitive (i.e. Number, Boolean, String…), This is basically arrays and objects. If you’re wondering why String is a primitive, it’s because in Javascript strings are immutable. 1> Aurel Bílý. .: Because the array itself has not changed, it is still the same array. In C-like languages, something similar to this is a constant pointer (to some address in memory) – You cannot change where the pointer points, but the memory can write the same content. In Javascript, this pointer-like behavior applies to anything that is not primitive (i.e. Number, Boolean, String…), This is basically arrays and objects. If you’re wondering why String is a primitive, it’s because strings are immutable in Javascript.

In JavaScript, how to round a number to the nearest 100/1000 based on a number?

I have a number that can be 2 digits like 67, 24, 82 or 3 digits like 556, 955, 865 or 4 digits and so on. How can I round a number to the nearest n+1 digits based on the number ? Example: roundup(87) => 100, roundup(776) => 1000, roudnup(2333) => 10000 Wait. 1> Nina Scholz..: You can take the logarithm of ten and round the value to get This value. function roundup(v) { return Math.pow(10, Math.ceil(Math.log10(v))); } console.log(roundup(87)); // 100 console.log(roundup(776)); // 1000 console.log(roundup(2333)); // 10000 2> Jonas Wilms..: const roundup = n => 10 ** (“” + n). length Just use the number of characters.

In javascript, how does the following code work

In Javascript, how the following code works. var a = { prop1: “a”, prop2: “b”, fun: function() { return this.prop1 + ” ” + this.prop2; } } var a2 = a; a.fn = “v”; a = {}; if (a === a2) { console.log(true); } else { console.log(false); } The above code prints errors. However, if I comment out the a = {} line, the value printed on the console is true. var a = { prop1: “a”, prop2: “b”, fun: function() { return this.prop1 + ” ” + this.prop2; } } var a2 = a; a.fn = “v”; //a={}; if (a === a2) { console.log(true); } else { console.log(false); } How the above code works because both variables (a and a2) point to the same object but when I initialize with {} it gives false. 1> T.J. Crowder..: …Because both variables (a and a2) point to the same object… They don’t do that anymore, as shown below: a={}; At this point, a2 refers to the old object, and a refers to a new, different object. a2 = a does not create a persistent link of any kind between variable a2 and variablea. Let’s throw in some Unicode art: After running…

Why, in JavaScript, is “2<<-1" zero instead of one?

I encountered this strange behavior of the bitwise left shift operator and I would like to understand it better… Suppose we want to build a function that receives an integer and returns the associated integer 2, that is: power => Math.pow(2, power) A more efficient approach is to use the shift-left bitwise operator (assuming overflow is not an issue): power => 1 <<power This works great. Oddly enough, this should also work: power => 2 <<(power-1) Because it comes from: 2 == 1 <<1 (Bitwise encoding 2) (a <<b) <<c == a <<(b + c) (Semantic <<) But this is not the case because: 2 <<-1 == 0 So, the second law fails: 0 == 2 <<-1 == (1 <<1) <<-1 != 1 <<(1 + -1 ) == 1 <<0 == 1 At first I thought shifting by negative numbers was a problem, maybe js interprets any shifting of negative numbers as zero? However, this is not the case, for example: 1 <<-31 == 2 As expected. More importantly: 2 <<31 == 2 <<-1 == 0 So…what’s going on here? Testing all shift values ​​2, all of them produce the expected values, except for numbers consistent with -1 mod 32,…

In JavaScript, how/should I use XMLHttpRequest’s async/await?

1> Thắng Trần X..: I usually do async/await like this: async function doAjaxThings() { // await code here let result = await makeRequest(“GET”, url); // code below here will only execute when await makeRequest() finished loading console.log(result); } document.addEventListener(“DOMContentLoaded”, function () { doAjaxThings(); // create and manipulate your DOM here. doAjaxThings() will run asynchronously and not block your DOM rendering document.createElement(“…”); document.getElementById(“…”).addEventListener(…); }); Promote xhr features here: function makeRequest(method, url) { return new Promise(function (resolve, reject) { let xhr = new XMLHttpRequest(); xhr.open(method, url); xhr.Onload= function () { if (this.status >= 200 && this.status <300) { resolve(xhr.response); } else { reject({ status: this.status, statusText: xhr.statusText }); } }; xhr.Onerror= function () { reject({ status: this.status, statusText: xhr.statusText }); }; xhr.send(); }); } 2> Ron Royston..: I created hope for XHR. Then just use await inside the async function to call it. function getHTML(url) { return new Promise(function (resolve, reject) { var xhr = new XMLHttpRequest(); xhr.open(‘get’, url, true); xhr.respOnseType= ‘document’; xhr.Onload= function () { var status = xhr.status; if (status == 200) { resolve(xhr.response.documentElement.innerHTML); } else { reject(status); } }; xhr.send(); }); } async function schemaPageHandler(){ try { var parser = new window.DOMParser(); var remoteCode = await getHTML(‘https://schema.org/docs/full.html’); var sourceDoc…

In Javascript, how to conditionally update an object’s properties?

I have seen this post and was wondering if there is a way to write a function using Javascript that can conditionally update all properties of an object by using another object with new values? Say I have this object: let oldObj = {test1: ‘valueTest1’,test2: ‘valueTest2’,test3: ‘valueTest3’ }; I want to update the value of another object using its passed properties, like this: let newObj = {test2: ‘newValue2′} In this case, I only want to update the property “test2”. If I knew the property I wanted to update and I knew the original property beforehand, I would use: oldObj = { …(newObj.test1) && {test1: newObj.test1} || {test1: oldObj.test1}, …(newObj.test2) && {test2: newObj.test2} || {test2: oldObj.test2}, …(newObj.test3) && {test3: newObj.test3} || {test3: oldObj.test3}, }; Meaning I will only update the value when the property goes into the new object, but of course I have to add as many conditions as there are properties in the object. This approach is good, but it doesn’t generalize, so if the object has 10 properties, I will have to write out 10 conditions. Is there a way to write a function that can conditionally update a property so that I don’t have to write 10…

In JavaScript, how to define an array?

In Javascript, what is the way to define an array? Define an array variable: var a=new Array(); specify the size when defining: var a=new Array(20); assign an initial value when defining: var a=new Array(‘abc’, ‘def’ , ‘ghi’, ‘opq’); How to determine whether an object is an array in js Before explaining how to determine whether an object is an array type, let us first consolidate the data types of js. There are a total of js Six major data types: number, string, object, Boolean, null, and undefined. In addition to the first four types, null, object, and array return all object types; for function types, function returns are used, such as typeof(Date), typeof(eval), etc. Let’s get to the point, how to determine the array type in js. instanceof is used to determine whether a variable is an instance of an object. The operand on the left is an object, and the operand on the right is a function object or function constructor. The principle is to determine whether the prototype chain of the object of the left operand has the prototype attribute of the constructor of the right operand. a instanceof b?alert(“true”):alert(“false”) //Note that the b value is the data…

In JavaScript, the Array object has methods that do not include

The methods owned by the Array object in Javascript do not include Methods of the Array object FF: Firefox, N: Netscape, IE: Internet Explorer method description FF N IE concat() connects two or more array and returns the result. 1 4 4 join() puts all elements of the array into a string. Elements are separated by the specified delimiter. 1 3 4 pop() removes and returns the last element of the array 1 4 5.5 push() adds one or more elements to the end of the array and returns the new length. 1 4 5.5 reverse() Reverse the order of elements in an array. 1 3 4 shift() Removes and returns the first element of an array 1 4 5.5 slice() Returns the selected element from an existing array 1 4 4 sort() Sorts the elements of an array 1 3 4 splice( ) removes elements and adds new elements to the array. 1 4 5.5 toSource() represents the source code of the object 1 4 – toString() converts the array to a string and returns the result. 1 3 4 toLocaleString() Convert the array to a local array and return the result. 1 3 4 unshift() adds one or…

In JavaScript, are constructors in classes necessary?

I’m reading the Javascript classes in the Mozilla documentation section “Class Body and Method Definitions”. Under the Constructors section it says this A constructor method is a special method used to create and initialize objects created using a class. There can be only one special method named “constructor” in a class. If a class contains multiple constructor methods, then Throws SyntaxError. The constructor can use the super keyword to call the superclass’s constructor. From the above statement, I can confirm that we cannot have multiple constructors. But it does not mention whether constructors are required in class declarations/expressions in Javascript. 1> Suresh Atta..: You should just write a class without a constructor and see if it works 🙂 From same docs As mentioned above, if no constructor method is specified, the default constructor is used. For base classes, the default constructor is: constructor() {} For derived classes, the default constructor is: constructor(…args) { super(…args); }

Contact Us

Contact us

181-3619-1160

Online consultation: QQ交谈

E-mail: [email protected]

Working hours: Monday to Friday, 9:00-17:30, holidays off

Follow wechat
Scan wechat and follow us

Scan wechat and follow us

Follow Weibo
Back to top
首页
微信
电话
搜索