part3 JavaScript object (this is serious)

Posted by gpittingale on Tue, 22 Feb 2022 15:56:20 +0100

Everything is object

  • A Boolean can be an object.
  • The numeric type can be an object.
  • A string can also be an object
  • Date is an object
  • Math and regular expressions are also objects
  • An array is an object
  • Functions can also be objects

Access method

objectName.methodName()
  1. Convert case

    var x=message.toUpperCase();
    var x=message.toLowerCase();
    

create object

  • Use Object to define and create an instance of the Object.

    • In JavaScript, almost all objects are instances of Object type, and they are all from Object Prototype inherits properties and methods.

      The Object constructor creates an Object wrapper.

      The Object constructor will create an Object according to the given parameters. The specific conditions are as follows:

      • If the given value is null or undefined, an empty object will be created and returned.

      • If a value of a basic type is passed in, an object of its wrapper type will be constructed.

      • If a value of reference type is passed in, this value will still be returned, and the variable copied by them will keep the same reference address as the source object.

      • When called in a non constructor form, the behavior of Object is equivalent to new Object().

        new Object([value])// Called as a constructor
        
  • Use the function to define the object, and then create a new object instance.

public instance constructors

function person(firstname,lastname,age,eyecolor)
{
    this.firstname=firstname;
    this.lastname=lastname;
    this.age=age;
    this.eyecolor=eyecolor;
}

Add attribute

person.firstname="John";
person.lastname="Doe";
person.age=30;
person.eyecolor="blue";

Add method

function person(firstname,lastname,age,eyecolor)
{
    this.firstname=firstname;
    this.lastname=lastname;
    this.age=age;
    this.eyecolor=eyecolor;

    this.changeName=changeName;
    function changeName(name)
    {
        this.lastname=name;
    }
}

Date

getFullYear() Gets the year.

getTime() Returns the number of milliseconds since January 1, 1970.

setFullYear() Set a specific date.

toUTCString() Converts the date of the current day (based on UTC) to a string.

getDay() And arrays to show weeks, not just numbers.

Display a clock Display a clock on the web page.

classification

Prototype (prototype object)

All javaScript objects inherit properties and methods from a prototype

  • Date object from date Prototype inheritance.
  • Array object from array Prototype inheritance.
  • The Person object is from Person Prototype inheritance.

Add new properties or methods to all existing objects.

function Person(first, last, age, eyecolor) {
  this.firstName = first;
  this.lastName = last;
  this.age = age;
  this.eyeColor = eyecolor;
}
 
Person.prototype.nationality = "English";

Using the prototype attribute, you can add new methods to the constructor of the object:

function Person(first, last, age, eyecolor) {
  this.firstName = first;
  this.lastName = last;
  this.age = age;
  this.eyeColor = eyecolor;
}
 
Person.prototype.name = function() {
  return this.firstName + " " + this.lastName;
};

Number object

  • All 64 bit
  • Infinity
  • NaN (non numeric value)

String object

  • length
  • Find character indexOf()
  • Content match()
  • Replace content
  • String case conversion toUpperCase() / toLowerCase():
  • Convert string to array split()

Date

getFullYear() Gets the year.

getTime() Returns the number of milliseconds since January 1, 1970.

setFullYear() Set a specific date.

toUTCString() Converts the date of the current day (based on UTC) to a string.

getDay() And arrays to show weeks, not just numbers.

Display a clock Display a clock on the web page.

Array

establish

1: General method:

var myCars=new Array();
myCars[0]="Saab";   
myCars[1]="Volvo";
myCars[2]="BMW";

2: Concise way:

var myCars=new Array("Saab","Volvo","BMW");

3: Literally:

var myCars=["Saab","Volvo","BMW"];

visit

var name=myCars[0];
myCars[0]="Opel";

Boolean

Rookie tutorial online editor (runoob.com)

Math

round()
random()
max()
min()

RegExp

JavaScript RegExp object | rookie tutorial (runoob.com)

JSON

  • Full English name of JSON JavaScript Object Notation
  • JSON is a lightweight data exchange format.
  • JSON is a stand-alone language*****
  • JSON is easy to understand.

JSON uses JavaScript syntax, but the JSON format is just a text.

rule of grammar

  • The data is a key / value pair.
  • Data is separated by commas.
  • Brace save object
  • Square brackets hold the array
  1. "name":"Runoob"

  2. {"name":"Runoob", "url":"www.baidu.com"}

  3. "sites":[
        {"name":"Runoob", "url":"www.runoob.com"}, 
        {"name":"Google", "url":"www.google.com"},
        {"name":"Taobao", "url":"www.taobao.com"}
    ]
    
  4. var text = '{ "sites" : [' +
    '{ "name":"Runoob" , "url":"www.runoob.com" },' +
    '{ "name":"Google" , "url":"www.google.com" },' +
    '{ "name":"Taobao" , "url":"www.taobao.com" } ]}';
    

Topics: Javascript