ecmascript-6

JavaScript split string with backslash

百般思念 提交于 2021-02-17 07:17:09
问题 I get from server some path like that: \some\some\some\some\mainSome And for display it to front, I need only last path(mainSome). And try to split it, but I can't. const path = '\some\some\some\some\mainSome'.split('\') //And also tried const path = '\some\some\some\some\mainSome'.split('\\') And this didn't work. Waiting for help from you 回答1: try this String.raw`\some\some\some\some\mainSome\`.split("\\"); 回答2: It's actually escaping the s s and m s in the string - you need to have a

Select all HTML elements with text

|▌冷眼眸甩不掉的悲伤 提交于 2021-02-17 07:13:32
问题 I have HTML document: <div class="blog"> <div class="image"></div> <div class="content"> <p class="text"> Some text </p> <div class="date">23.12</div> </div> </div> How can i get all elements with inner text (.text, .date), but not wrapper (.content). It's just an example, in real situation i don't know real HTML structure, but i need a method how select all elements with text, except their wrappers. Need vanilla way help, without jquery. 回答1: //first select the container var container =

String template - new line doesn't show

别来无恙 提交于 2021-02-17 06:13:08
问题 I'm trying to use a template string to update infoDiv's text and for some reason all this text is in one line. What am I doing wrong? None of these solutions work - they're just rendered. infoDiv.textContent = `Hi! It's a ${counter} question quiz. It takes about ${time_minutes} minutes to solve it. Are you ready?` EDIT: I found a CSS solution here ( white-space: pre-line; ). let counter = 8; let time_minutes = 3; infoDiv.textContent = `Hi! It's a ${counter} question quiz. It takes about $

Resolving typescript modules in the browser yields a 404

偶尔善良 提交于 2021-02-17 05:46:46
问题 I am looking to put together a small example of for a web app that is created using typescript . I have an issue with importing a module. I just want information on why this is wrong and what options I have to sort it. The problem is a 404 for the in the index.js file when trying to import the hello. Here is the code: index.html <!DOCTYPE html> <html> <head> <title>Hello World</title> </head> <body> <p>Check the console log...</p> </body> <script type="module" src="/dist/index.js"></script> <

Find all the words in a string which have the greatest length

会有一股神秘感。 提交于 2021-02-16 19:50:17
问题 I want to find all the words from the string which have the greatest length. At the moment, the result is just the first with the greatest length: 'jumped1' , whereas I want them all: ['jumped1', 'jumped2'] . How can I adapt the following? function test(str) { var newStr = str.split(' '); var nu = 0; var word =null; for(var i=0; i < newStr.length; i++){ if(newStr[i].length > nu){ nu = newStr[i].length; // length word = newStr[i]; // word } } return word; } console.log(test("The quick brown

What is the difference (if any) between the different function declarations within JavaScript objects?

删除回忆录丶 提交于 2021-02-16 17:11:10
问题 I have a JavaScript object: var methods = { classStyle() { console.log('Class style function'); }, traditionalStyle: function() { console.log('Traditional style function'); }, arrowStyle: () => { console.log('Arrow style function'); } }; methods.classStyle(); methods.traditionalStyle(); methods.arrowStyle(); The output is as expected: (index):70 Class style function (index):74 Traditional style function (index):78 Arrow style function My questions are: Is there any difference at all between

What is the difference (if any) between the different function declarations within JavaScript objects?

主宰稳场 提交于 2021-02-16 17:09:53
问题 I have a JavaScript object: var methods = { classStyle() { console.log('Class style function'); }, traditionalStyle: function() { console.log('Traditional style function'); }, arrowStyle: () => { console.log('Arrow style function'); } }; methods.classStyle(); methods.traditionalStyle(); methods.arrowStyle(); The output is as expected: (index):70 Class style function (index):74 Traditional style function (index):78 Arrow style function My questions are: Is there any difference at all between

What is the difference (if any) between the different function declarations within JavaScript objects?

ぐ巨炮叔叔 提交于 2021-02-16 17:08:11
问题 I have a JavaScript object: var methods = { classStyle() { console.log('Class style function'); }, traditionalStyle: function() { console.log('Traditional style function'); }, arrowStyle: () => { console.log('Arrow style function'); } }; methods.classStyle(); methods.traditionalStyle(); methods.arrowStyle(); The output is as expected: (index):70 Class style function (index):74 Traditional style function (index):78 Arrow style function My questions are: Is there any difference at all between

Mutable difference between ES6 named and default exports

只愿长相守 提交于 2021-02-16 16:38:10
问题 When importing/exporting data from ES6 modules, mutability of that data appears to be different between named imports and exports. Is there a reason for this or some fundamental difference I'm not understanding? // counter.js export let count = 0; export const incrementCount = () => count += 1; export default count; // main-default.js import count, { incrementCount } from './counter'; console.log(count); // 0 incrementCount(); incrementCount(); console.log(count); // 0 // main-named.js import

Mutable difference between ES6 named and default exports

风流意气都作罢 提交于 2021-02-16 16:38:09
问题 When importing/exporting data from ES6 modules, mutability of that data appears to be different between named imports and exports. Is there a reason for this or some fundamental difference I'm not understanding? // counter.js export let count = 0; export const incrementCount = () => count += 1; export default count; // main-default.js import count, { incrementCount } from './counter'; console.log(count); // 0 incrementCount(); incrementCount(); console.log(count); // 0 // main-named.js import