function
this
The value of this depends on the mode being invoked. There are four invocation modes in JavaScript
- Method invocation pattern
- Function call pattern
- Constructor invoke pattern
- Apply call pattern
Method invocation pattern
When a function is stored as a property of an object, we call it a method. This is bound to the object when the method is called
Function call pattern
This is bound to the global object when called in function call mode
Constructor invoke pattern
This is bound to the new object
Apply call pattern
Apply takes two parameters, the this value to be bound and an array of parameters.
parameter
JS does not care how many arguments a function takes. Arguments are passed like Array, i.e. arguments[0] can be called to access the first argument
function doAdd()
{
if(arguments.length==1)
{
// do something
}
else
{
// do something}}Copy the code
Therefore, JS functions cannot be overloaded
You pass
JS functions are passed by value. Pass a primitive type value that copies a local variable. When a reference type is passed, the memory address is copied to the local variable
Each environment can search up the scope chain, but not down. The chain of scopes can be extended using the with statement
function buildurl()
{
var qs='xxx'
with(location){
var url=href+qs
}
return url
}
Copy the code
scope
When a var declares a variable, it is automatically added to the nearest environment. Initialization does not add var declarations to global variables
Object-oriented programming
object
JS defines an object as an unordered collection of properties that can contain base values, objects, or functions.
JS has two kinds of attributes: data attribute and accessor attribute
Data attributes
- [Configurable]
- [Enumerable]
- [Writable]
- [Value]
You must modify default properties using Object.defineProperty()
Once a property is defined as unconfigurable, it cannot be modified to be configurable
Accessor properties
- [Configurable]
- [Enumerable]
- [[Get]]
- [[Set]]
Define multiple attributes at once using Object.defineProperties()