开发者

Javascript, what does the 1 in slice(1) refer to in this program?

开发者 https://www.devze.com 2023-02-13 03:03 出处:网络
In this program, I understand (I think) that paragraph.charAT(0) = \"%\" checks whether the first character in paragraph is equal to %, i.e. the counting starts at 0, so charAT(0) is the first charact

In this program, I understand (I think) that paragraph.charAT(0) = "%" checks whether the first character in paragraph is equal to %, i.e. the counting starts at 0, so charAT(0) is the first character

However, in the line, paragraph.slice(1), what does the 1 refer to? Is it slicing off the first character?, which in this case will be at 0 position?

function processParagraph(paragraph) {
  var header = 0;
  while (paragraph.charAt(0) == "%") {
    paragraph = paragraph.slice(1);
    header++;
  }

  return {type: (header == 0 ? "p" : "h" +开发者_StackOverflow中文版 header),
          content: paragraph};
}

show(processParagraph(paragraphs[0]));


It extracts a substring starting at index 1 (2nd character) of the paragraph string.

For example, consider this:

var paragraph = "Hi my name is Russell";
console.log( paragraph.slice(1) ); //returns 'i my name is Russell'


.slice

string.slice(beginslice[, endSlice])

Extracts a section of a string and returns a new string.

It returns everything after the first character, essentially cutting the first character off.


It removes the first character from the string and returns that without altering the original string. I recommend you look at the documentation for slice.


it's slicing off the first character (which is a "%")

0

精彩评论

暂无评论...
验证码 换一张
取 消