Home > Web Front-end > JS Tutorial > body text

Introduction to objects in javascript (with code)

不言
Release: 2019-03-23 11:33:10
forward
2076 people have browsed it

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;
Copy after login

The value of x is 12

Method

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

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";
Copy after login

Or use the following method

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

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");
Copy after login

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");
Copy after login

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!

Related labels:
source:csdn.net
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template