This is the third day of my August challenge

object

In JavaScript, everything is an object, and all data can be stored in an object. No matter what data type is declared, it has properties and methods.

Create an object

There are two ways to create an object.

  • Create an assignment using a new Object or curly braces.

    var obj = new Object(a); obj.name="Zhang";
    obj.age = 33;
    obj.sex ="Male";
    console.log(obj);// Result is {name:" zhang SAN ",age:33,sex:" male "}
    Copy the code
    // Assign values directly when created, in the form of curly braces.
    var obj = {name:"Zhang".age:33.sex:"Male"};
    console.log(obj);// Result is {name:" zhang SAN ",age:33,sex:" male "}
    Copy the code
  • Use the object constructor

    Construct objects for functions

    function person(name,age,sex){
      // Create object properties
      this.name=name;
      this.age=age;
      this.sex=sex;
      // Create a method
      this.eat = eat;
      function eat(){
        console.log(name+"Here we go."); }}var p = new person("Zhang".33."Male");
    console.log(p);//person {name: "zhang ", age: 33, sex:" male "}
    p.eat();// Zhang SAN has dinner
    Copy the code

Access the properties and methods of an object

Like other object-oriented languages, objects are used. Property name Gets the property.

var a = "hello";
// Gets the length of the string object
console.log(a.length);// The result is 2.
// Call the string uppercase method to uppercase all characters.
console.log(a.toUpperCase());
Copy the code

Window

sessionStorage

Kv structure data can be stored in the current window, the data will disappear after the web page is closed.

/ / store
window.sessionStorage.setItem("key",value)
/ / to get
window.sessionStorage.getItem("key")
/ / delete
window.sessionStorage.removeItem("key")
/ / to empty
windows.sessionStorage.clear()

Copy the code

localStorage

It can permanently save the data of KV structure. Use the same as sesstionStorage.

/ / store
window.localStorage.setItem("key",value)
/ / to get
window.localStorage.getItem("key")
/ / delete
window.localStorage.removeItem("key")
/ / to empty
windows.localStorage.clear()
Copy the code

Pseudo array

A pseudo-array is still an object, but its internal property definitions make it look like an array.

Rules:

  • Has the length attribute, and all other attributes must be non-negative integers or non-negative integers as strings.
  • You cannot have methods that the array itself owns.

The sample

var fakeArray={
  "0":"Zhang"."1":"Bill"."2":"Fifty"."length":3
}
Copy the code

Common pseudo-arrays in javascript include arguments and DOM object collections

The purpose of using pseudo-arrays is to enable objects to use some array methods.

this

In general, this refers to whoever called the function.

By default, after a normal function call, this is the Window object.

To change this

Use the call function to change this, passing the specified value into the call function.

function test(){
	console.log(this)}var p = {name:"Zhang".age:33}
test.call(p)
Object {name:" zhang SAN ",age:33}
Copy the code