Written in the beginning

  • The more advanced positions, engaged in the bottom of the work, the more you need a solid foundation of knowledge, I will not make so many handwritten source code tutorials as before, will be more focused on the basis of
  • Tomorrow is1024, I will send to the official account [100 red envelopes in cash】,Don't focus onYou can also participate in the raffle, just remember to participate. (Not in order to publicize the public number, it is simple to send a welfare, let everyone happy and lively, many number of the Lord also often draw my gift)
  • Today’s study isstring

The official start of the

  • Why study strings
    • becauseJSThere is one special point:typeof nullAs a result,object
  • Another reason is that strings can have methods, but they can’t set properties. Let’s reveal it in code
    const str = 'Top end Peter Teacher is awesome'
    str.xxoo = 'Are you paying attention? '
    console.log(str.xxoo, 'xxoo')
Copy the code
  • Result output:undefined
  • Why is that?
    • Because strings are fundamental data types, see below.

But why do strings have methods?

  • Speak in code:
    const str = 'Top end Peter Teacher is awesome'
    console.log(str.substring(1), 'xxoo')
Copy the code
  • Result output: Teacher Peter at the end of the peak is awesome

  • Highlight: why does this string have a method? It’s just a string!

The legacy create create string method

  • There’s another way to create strings, which we forgot.
    const str = new Object('Top end Peter Teacher is awesome')
    console.log(typeof str, 'str')
Copy the code
  • Print result:obect
  • throughnew ObjectCreate the string, unexpectedlytypeofAs a result,object“, so I wonder, is there a case where accessing a string automatically translates to this?

Your answer

  • Once we have created the string, we use the usual form for example:Const STR = 'STR', and then to call its method, there will be the following steps:
    • Declaration string:Const STR = 'STR'
    • To access its properties (methods) :str.substring(1)
    • Read mode accessstrThis string, it’s going to take a couple of steps
      • New String returns an instance
      • On the instancesubstringmethods
      • Destroy instance
      • As I mentioned in an earlier article,JSOnce strings are created, their values cannot be changed. To change the saved string of a variable, first destroy the original string and then populate the variable with another string containing the new value
      • So the above code would actually run like this:
Original code:const str = Peter teachers' 666 '
str.substring(1)
The real internal implementation is:let str = new String(Peter teachers' 666 ')
let str1 = str.substring(1) str = null Copy the code

Important: once strings in JS are created, their values cannot be changed. To change the saved string of a variable, first destroy the original string and then populate the variable with another string containing the new value

So why doesn’t setting attributes to strings work?

  • We declare:Const STR = 'front-end peak'
  • And then you set properties for itstr.xxoo = 'xxoo'
  • Then we read its propertiesstr.xxoo
  • What happened during this time:
    • str.xxoo = 'xxoo'First, an instance is created and set on the instancexxooThe value of the attribute is:xxoo
    • When this line of code completes, the instance is destroyed
    • So when we visitstr“, does not have this property

This is called a primitive value wrapper type, and its declaration cycle is destroyed as soon as the line of code that accesses it executes. That’s how it differs from a reference type. The instance we create with new String() is a reference type, so it’s an Object.

The instance created by new String is an object (reference type)

  • Code:
    const str = new String('Top end Peter Teacher is awesome')
    console.log(str, 'str')
Copy the code

  • sotypeof strThe result is:object

Write in the last

  • If there is a bad place to write, you can point out in the comments below, usually busy, later will try to write some basic, the bottom forgotten knowledge points, do not forget the original intention, write the article in order to let more people learn what they want
  • If it feels good, click on itLike/watching/followingBar, official number: [The front-end peak]

This article is formatted using MDNICE

– END –