Home  >  Article  >  Web Front-end  >  Introduction to objects in javascript (with code)

Introduction to objects in javascript (with code)

不言
不言forward
2019-03-23 11:33:102008browse

This article brings you an introduction to objects in JavaScript (with code). It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.

Everything in JavaScript is an object, and JavaScript allows custom objects.

The object has properties and methods

Properties

var message = "hello world!";var x=message.length;

The value of x is 12

Method

var message="hello world!";var x=message.toUpperCase();

The value of x is "HELLO WORLD!"

Creating objects

Two methods

Define and create instances of objects

Use functions to define objects and then create new object instances

Create an instance directly

student=new Object();
student.name="Sine";
student.age=20;
student.school="XX University";

Or use the following method

student=new Object();student={
    name:"Sine",
    age:20,
    school:"XX University"};

Use functions to create

function register(name,age,school){
    this.name=name;
    this.age=age;
    this.school=school;
    }
    student = new register("Sine",20,"XX University");

Add methods to the object

In The method of defining objects inside the constructor function

function register(name,age,school){
    this.name=name;
    this.age=age;
    this.school=school;
    function ChangeName(name)
    {
        this.name=name;
    }}
student=new register("Sine",20,"XX University");
student.ChangeName("Cosine");

This article has ended here. For more other exciting content, you can pay attention to the JavaScript Video Tutorial column of the PHP Chinese website!

The above is the detailed content of Introduction to objects in javascript (with code). For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:csdn.net. If there is any infringement, please contact admin@php.cn delete