What is a js object? Introduction to js objects (with code)
This article brings you what is a js object? The introduction of js objects (with code) has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.
What is an object
In JavaScript, an object is like an entity with separate properties and types. A cup is an object. The cup has attributes such as color and weight. Likewise, a JavaScript object has properties that define its characteristics.
A method is a function associated with an object, or in other words, a method is an object property whose value is a function.
Objects can be divided into the following categories
Built-in objects/native objects
are predefined objects in the JavaScript language
Host object
is an object provided by the JavaScript running environment
Custom object
It is an object created by the developer independently
Object object
The Object type is a reference type. But the Object type is the parent of all types in JavaScript (all types of objects can be the properties and methods of Object)
Creating objects
/* * 1. 对象的初始化器创建方式 * var 对象名={ * 属性名 : 属性值 * 方法名 : function{ * 方法体 * } * } */ var obj = { name : '九筒', age : 2, sayYou : function () { console.log('火锅') } }; /* 2. 对象的构造函数方式 * * 利用所有的引用类型创建对应的对象->具有具体的类型 * var num = new Number;//number类型 * var str = new String;//string类型 * var boo = new Boolean;//boolean类型 * * 利用Object作为构造函数创建对象 * var 对象名 = new Object(); * var 对象名 = Object(); */ var num = new Number(); var str = new Storage(); var boo = new Boolean(); var obj2 = new Object(); var obj3 = Object(); /* 利用Object.create创建对象 * var 对象名 = Object.create(null) -> 创建一个空对象 var 对象名 = Object.create(obj) * obj - 表示另一个对象 * 特点 - 当前创建的新对象拥有与obj对象相同的属性和方法*/ var obj4 = Object.create(null); var obj5 = Object.create(obj);
Properties of objects
Define the properties of the object
The properties of the object are like variables attached to the object
/*对象介意定义多个属性 * 属性之间使用逗号分开*/ var obj = { name : '吴凡', age : 23, book : function () { console.log('暗毁') } };
Calling the properties of the object
/* 调用对象的属性 * 对象名.属性名 * 不适用于复杂命名的属性名称*/ console.log(obj.name); /* 对象名[属性名]-通用方式 适用于复杂命名的属性名称 * */ console.log(obj['name']);//属性名是字符串形式
Add, delete, and modify the properties of the object
var obj = { name : '火锅', variety : '比熊', age : function () { console.log('3') } } /* 新增对象的属性 * 1对象名.新的属性名 = 属性值 * 2对象名[新的属性名] = 属性值*/ obj.col='白色'; console.log(obj.col);//白色 /*删除对象的属性 * delete 对象名.属性名 * delete 对象名[属性名]*/ delete obj.col console.log(obj.col);//undefined /*修改对象的属性 * 对象名.已存在的属性名 = 属性值 * 对象名[已存在的属性名] = 属性值*/ obj.name = '九筒'; console.log(obj.name);//九筒
Detecting the properties of objects
var obj = { name : '火锅', variety : '比熊', age : function () { console.log('3') } }; console.log(obj.name) /* 1. 判断对象的属性值是否为undefined*/ if (obj.name !==undefined){ console.log('obj对象name属性存在') }else{ console.log('obj对象name属性不存在') } /* 2. 判断对象的属性值,先转换为boolean类型*/ if (obj.name){ console.log('obj对象name属性存在') } /* 3. 利用in关键字进行判断*/ if ('name' in obj){ console.log('obj对象name属性存在') }else{ console.log('obj对象name属性不存在') } /* 4. Object类型提供了hasOwnProperty()方法*/ if (obj.hasOwnProperty('name')){ console.log('obj对象name属性存在') }else{ console.log('obj对象name属性不存在') }
Convenience properties
var obj = { name : '小薄荷', age : '0.3', variety : function () { console.log('萨摩耶') } }; // 1.for...in语句 for (var objAttr in obj) { // 通过对象属性或方法对应的值的类型进行区别 if (obj[objAttr] instanceof Function) { // 当前是对象的方法 obj[objAttr](); } else { // 当前是对象的属性 console.log(obj[objAttr]); } } // 2.Object类型提供了keys()方法 - 只能遍历可枚举的属性 var arr = Object.keys(obj); for (var v in arr) { var objAttr = arr[v]; // 通过对象属性或方法对应的值的类型进行区别 if (obj[objAttr] instanceof Function) { // 当前是对象的方法 obj[objAttr](); } else { // 当前是对象的属性 console.log(obj[objAttr]); } } // 3.Object类型提供了getOwnPropertyNames()方法 - 包括不可枚举的属性 var arr = Object.getOwnPropertyNames(obj); for (var v in arr) { var objAttr = arr[v]; // 通过对象属性或方法对应的值的类型进行区别 if (obj[objAttr] instanceof Function) { // 当前是对象的方法 obj[objAttr](); } else { // 当前是对象的属性 console.log(obj[objAttr]); } }
Methods of objects
Methods for calling, adding, modifying, and deleting objects
The methods and attributes for calling, adding, modifying, and deleting objects are basically the same
var obj = { name : '火锅', variety : '比熊', age : function () { console.log('3') } } /*调用对象的方法*/ // 1.对象名.方法名() obj.sayMe(); // 2.对象名[方法名]() obj['sayMe'](); /*新增对象的方法*/ // 1.对象名.新的方法名 = function(){} obj.name = function(){ console.log('九筒'); } console.log(obj); // 2.对象名[新的方法名] = function(){} /*修改对象的方法*/ // 1.对象名.方法名 = function(){} obj.name = function(){ console.log('九筒'); } // 2.对象名[方法名] = function(){} /*删除对象的方法*/ //1.delete 对象名.方法名 delete obj.sayMe; // 访问对象中不存在的方法 -> 报错(TypeError: obj.sayMe is not a function) // obj.sayMe(); // 2.delete 对象名[方法名]
Related recommendations:
Summary of character methods and string operation methods in js (with code )
Analysis of common problems in the http proxy library http-proxy in nodejs
The above is the detailed content of What is a js object? Introduction to js objects (with code). For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



How to use WebSocket and JavaScript to implement an online speech recognition system Introduction: With the continuous development of technology, speech recognition technology has become an important part of the field of artificial intelligence. The online speech recognition system based on WebSocket and JavaScript has the characteristics of low latency, real-time and cross-platform, and has become a widely used solution. This article will introduce how to use WebSocket and JavaScript to implement an online speech recognition system.

WebSocket and JavaScript: Key technologies for realizing real-time monitoring systems Introduction: With the rapid development of Internet technology, real-time monitoring systems have been widely used in various fields. One of the key technologies to achieve real-time monitoring is the combination of WebSocket and JavaScript. This article will introduce the application of WebSocket and JavaScript in real-time monitoring systems, give code examples, and explain their implementation principles in detail. 1. WebSocket technology

Introduction to how to use JavaScript and WebSocket to implement a real-time online ordering system: With the popularity of the Internet and the advancement of technology, more and more restaurants have begun to provide online ordering services. In order to implement a real-time online ordering system, we can use JavaScript and WebSocket technology. WebSocket is a full-duplex communication protocol based on the TCP protocol, which can realize real-time two-way communication between the client and the server. In the real-time online ordering system, when the user selects dishes and places an order

How to use WebSocket and JavaScript to implement an online reservation system. In today's digital era, more and more businesses and services need to provide online reservation functions. It is crucial to implement an efficient and real-time online reservation system. This article will introduce how to use WebSocket and JavaScript to implement an online reservation system, and provide specific code examples. 1. What is WebSocket? WebSocket is a full-duplex method on a single TCP connection.

JavaScript and WebSocket: Building an efficient real-time weather forecast system Introduction: Today, the accuracy of weather forecasts is of great significance to daily life and decision-making. As technology develops, we can provide more accurate and reliable weather forecasts by obtaining weather data in real time. In this article, we will learn how to use JavaScript and WebSocket technology to build an efficient real-time weather forecast system. This article will demonstrate the implementation process through specific code examples. We

JavaScript tutorial: How to get HTTP status code, specific code examples are required. Preface: In web development, data interaction with the server is often involved. When communicating with the server, we often need to obtain the returned HTTP status code to determine whether the operation is successful, and perform corresponding processing based on different status codes. This article will teach you how to use JavaScript to obtain HTTP status codes and provide some practical code examples. Using XMLHttpRequest

Usage: In JavaScript, the insertBefore() method is used to insert a new node in the DOM tree. This method requires two parameters: the new node to be inserted and the reference node (that is, the node where the new node will be inserted).

JavaScript is a programming language widely used in web development, while WebSocket is a network protocol used for real-time communication. Combining the powerful functions of the two, we can create an efficient real-time image processing system. This article will introduce how to implement this system using JavaScript and WebSocket, and provide specific code examples. First, we need to clarify the requirements and goals of the real-time image processing system. Suppose we have a camera device that can collect real-time image data
