How to use js objects and prototypes
This time I will show you how to use js objects and prototypes, and what are the precautions for using js objects and prototypes. The following is a practical case, let's take a look.
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>title</title> <script> /* * * 复习: * * 面向过程和面向对象都是编程的思想,方式不一样 * 面向过程:凡事都是亲力亲为,所有的代码都要自己写,每一步都要很清楚,注重的是过程 * 面向对象:执行者成为指挥者,只要找对象,然后让对象做相关的事情,注重的是结果 * 面向对象的特性:封装,继承,多态 * 封装;就是代码的封装,把一些特征和行为封装在对象中. * 面向对象的编程思想 :根据需求,抽象出相关的对象,总结对象的特征和行为,把特征变成属性,行为变成方法,然后定义(js) 构造函数 ,实例化对象,通过对象调用 属性和方法 ,完成相应的需求.---编程的思想 * * 对象:具体特指的某个事物,有特征(属性)和行为(方法),对象可以看成是一坨无序属性的集合 * * 如何 创建对象 ? * 通过调用new Object(),还有{},自定义构造函数 * * 创建对象的方式 * 1. 调用系统Object()----->创建出来的对象都是Object类型的,不能很明确的指出这个对象是属于什么类型 * 2. 字面量的方式{}----->只能 创建一个对象 (一次只能创建一个) * * 3. 工厂模式 创建对象----->----->推论---->自定义构造函数的方式 * 自定义构造函数(优化后的工厂模式) * * 自定义构造函数创建对象:4件事 * 1.在内存中申请一块空闲的空间,存储创建的对象 * 2.this就是当前实例化的对象 * 3.设置对象中的属性和方法(为对象添加属性和方法,为属性和方法赋值) * 4.把创建后的对象返回 * 都是需要通过new的方式 * * * 什么是原型? * 构造函数中有一个属性prototype,是原型,程序员使用的 * 实例对象中有一个属性proto,是原型,浏览器使用的,不是很标准的, * 实例对象中的proto指向的就是该实例对象中的构造函数中的prototype * 构造函数中的prototype里面的属性或者方法,可以直接通过实例对象调用 * 正常的写法:实例对象.proto才能访问到构造函数中的prototype中的属性或者方法 * per.proto.eat();//proto不是标准的属性 * per.eat(); * 原型就是属性,而这个属性也是一个对象 * Person.prototype--->是属性 * Person.prototype.属性或者Person.ptototype.方法() * * 本身在构造函数中定义的属性和方法,当实例化对象的时候,实例对象中的属性和方法都是在自己的空间中存在的,如果是多个对象。这些属性和方法都会在单独的空间中存在,浪费内存空间,所以,为了数据共享,把想要节省空间的属性或者方法写在原型对象中,达到了数据共享,实现了节省内存空间 * * * * 原型的作用之一:数据共享,节省内存空间 * * 原型的写法: * 构造函数.prototype.属性=值 * 构造函数.prototype.方法=值---->函数.prototype,函数也是对象,所以,里面也有proto * 实例对象.prototype-------->实例对象中没有这个属性,只有proto(暂时的) * * 简单的原型的写法 * 缺陷:--->新的知识点---->原型直接指向{}---->就是一个对象,没有构造器 * 构造函数.prototype={ * 切记:如果这这种写法,要把构造器加上 * * }; * * * 通过原型为内置对象添加原型的属性或者方法----->原因: * 系统的内置对象的属性和方法可能不满足现在需求,所以,可以通过原型的方式加入属性或者方法,为了方便开发 * * 为内置对象的原型中添加属性和方法,那么这个内置对象的实例对象就可以直接使用了 * String.prototype.方法=匿名函数; * var str="哈哈"; * str.方法();---->实例对象可以直接调用原型中的属性或者方法 * * * * * * * * * * */ function Person(age) { this.age=age; } Person.prototype.sayHi=function () { }; var p=new Person(10); p.sayHi(); </script> </head> <body> </body> </html>
I believe you have mastered the method after reading the case in this article. For more exciting information, please pay attention to other related articles on the php Chinese website!
Recommended reading:
Detailed explanation of JS framework library use cases
How to use native js to create a starry sky effect
The above is the detailed content of How to use js objects and prototypes. 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











Here's how to convert a MySQL query result array into an object: Create an empty object array. Loop through the resulting array and create a new object for each row. Use a foreach loop to assign the key-value pairs of each row to the corresponding properties of the new object. Adds a new object to the object array. Close the database connection.

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

PHP functions can encapsulate data into a custom structure by returning an object using a return statement followed by an object instance. Syntax: functionget_object():object{}. This allows creating objects with custom properties and methods and processing data in the form of objects.

Introduction to the method of obtaining HTTP status code in JavaScript: In front-end development, we often need to deal with the interaction with the back-end interface, and HTTP status code is a very important part of it. Understanding and obtaining HTTP status codes helps us better handle the data returned by the interface. This article will introduce how to use JavaScript to obtain HTTP status codes and provide specific code examples. 1. What is HTTP status code? HTTP status code means that when the browser initiates a request to the server, the service

In C++, there are three points to note when a function returns an object: The life cycle of the object is managed by the caller to prevent memory leaks. Avoid dangling pointers and ensure the object remains valid after the function returns by dynamically allocating memory or returning the object itself. The compiler may optimize copy generation of the returned object to improve performance, but if the object is passed by value semantics, no copy generation is required.

Introducing the new map of Genshin Impact version 4.4. Friends, Genshin Impact 4.4 version also ushered in the Sea Lantern Festival in Liyue. At the same time, a new map area will be launched in version 4.4 called Shen Yu Valley. According to the information provided, Shen Yugu is actually part of Qiaoying Village, but players are more accustomed to calling it Shen Yugu. Now let me introduce the new map to you. Introduction to the new map of Genshin Impact version 4.4. Version 4.4 will open "Chenyu Valley·Shanggu", "Chenyu Valley·Nanling" and "Laixin Mountain" in the north of Liyue. Teleportation anchor points have been opened for travelers in "Chenyu Valley·Shanggu" . ※After completing the prologue of the Demon God Quest·Act 3: The Dragon and the Song of Freedom, the teleportation anchor point will be automatically unlocked. 2. Qiaoyingzhuang When the warm spring breeze once again caressed the mountains and fields of Chenyu, the fragrant

In PHP, an array is an ordered sequence, and elements are accessed by index; an object is an entity with properties and methods, created through the new keyword. Array access is via index, object access is via properties/methods. Array values are passed and object references are passed.

The difference between Java heap and stack and application scenario analysis require specific code examples. In Java programs, heap and stack are two commonly used data structures, and they assume different roles and functions in memory. Understanding the difference between heap and stack is crucial to writing efficient Java programs. First, let's take a look at the Java heap. The heap is an area used to store objects. All objects created in the program are stored in the heap. The heap is where memory is dynamically allocated and released while the program is running. It is not subject to any restrictions and can be automatically allocated and released as needed.
