Which file should be referenced by the prototype prototype extension in vue

String.prototype.startsWith = function (str) {
  if (str == null || str == "" || this.length == 0 || str.length > this.length) {
    return false;
  }

  if (this.substring(0, s.length) == str) {
    return true;
  } else {
    return false;
  }
  return true;
}-sharp-sharp-sharp 

the environmental background of the problems and what methods you have tried

I can"t put it directly in the script tag

related codes

/ / Please paste the code text below (do not replace the code with pictures)

what result do you expect? What is the error message actually seen?

Jan.08,2022

can be placed in main.js.
but look at your code, the ES6 string extends this function, and you don't have to write it yourself.
in addition, extending the existing JS type directly is not a recommended approach.


can be added to index.html, just as you introduce third-party scripts.

ES6 has related functions, why do you extend it? string extension includes (), startsWith (), endsWith ()-ECMAScript 6 getting started-Ruan Yifeng

Menu