Share some data about some read-only properties in javaScript.
- eg1
String values obtained using array associative methods are read-only attributes and cannot be modified
let a = 'string';
console.log(a[0]); //s
a[0] = 'q';// The second line of output remains unchanged
// Read-only attributes, unlike arrays
console.log(a);//string
console.log(a[0])//s
Copy the code
- eg2
CharAt () in a character object returns a specified character from a string, and an error is reported if assigned
let a = 'string';
console.log(a.charAt(0)) //s
a.charAt(0) = 'q';
//Invalid left-hand side in assignment
The charAt() method returns the specified character from a string
console.log(a)
Copy the code
- eg3
Htmlelement. offsetTop and htmlElement. offsetLeft are also read-only attributes. OffsetTop returns the distance of the current element relative to the top inner margin of its offsetParent element, which is a block-level element
- eg4
In addition to offsets, get the width and height of the page’s elements, which are also read-only offsetWidth, offsetHeight, clientWidth, clientHeight
- eg5
Methods such as getFullYear() are read-only properties, and there is usually a corresponding method that starts with set.
The purpose of setting the read-only property
The values of read-only attributes are generally the underlying data types and are stored in the stack, saving space in the heap. (Personal understanding,)