search
HomeWeb Front-endJS TutorialHow to use js objects and prototypes

How to use js objects and prototypes

Jun 04, 2018 pm 06:06 PM
javascriptprototypeobject

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!

Statement
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
JavaScript's Core: Is It Built on C or C  ?JavaScript's Core: Is It Built on C or C ?May 05, 2025 am 12:07 AM

JavaScriptisnotbuiltonCorC ;it'saninterpretedlanguagethatrunsonenginesoftenwritteninC .1)JavaScriptwasdesignedasalightweight,interpretedlanguageforwebbrowsers.2)EnginesevolvedfromsimpleinterpreterstoJITcompilers,typicallyinC ,improvingperformance.

JavaScript Applications: From Front-End to Back-EndJavaScript Applications: From Front-End to Back-EndMay 04, 2025 am 12:12 AM

JavaScript can be used for front-end and back-end development. The front-end enhances the user experience through DOM operations, and the back-end handles server tasks through Node.js. 1. Front-end example: Change the content of the web page text. 2. Backend example: Create a Node.js server.

Python vs. JavaScript: Which Language Should You Learn?Python vs. JavaScript: Which Language Should You Learn?May 03, 2025 am 12:10 AM

Choosing Python or JavaScript should be based on career development, learning curve and ecosystem: 1) Career development: Python is suitable for data science and back-end development, while JavaScript is suitable for front-end and full-stack development. 2) Learning curve: Python syntax is concise and suitable for beginners; JavaScript syntax is flexible. 3) Ecosystem: Python has rich scientific computing libraries, and JavaScript has a powerful front-end framework.

JavaScript Frameworks: Powering Modern Web DevelopmentJavaScript Frameworks: Powering Modern Web DevelopmentMay 02, 2025 am 12:04 AM

The power of the JavaScript framework lies in simplifying development, improving user experience and application performance. When choosing a framework, consider: 1. Project size and complexity, 2. Team experience, 3. Ecosystem and community support.

The Relationship Between JavaScript, C  , and BrowsersThe Relationship Between JavaScript, C , and BrowsersMay 01, 2025 am 12:06 AM

Introduction I know you may find it strange, what exactly does JavaScript, C and browser have to do? They seem to be unrelated, but in fact, they play a very important role in modern web development. Today we will discuss the close connection between these three. Through this article, you will learn how JavaScript runs in the browser, the role of C in the browser engine, and how they work together to drive rendering and interaction of web pages. We all know the relationship between JavaScript and browser. JavaScript is the core language of front-end development. It runs directly in the browser, making web pages vivid and interesting. Have you ever wondered why JavaScr

Node.js Streams with TypeScriptNode.js Streams with TypeScriptApr 30, 2025 am 08:22 AM

Node.js excels at efficient I/O, largely thanks to streams. Streams process data incrementally, avoiding memory overload—ideal for large files, network tasks, and real-time applications. Combining streams with TypeScript's type safety creates a powe

Python vs. JavaScript: Performance and Efficiency ConsiderationsPython vs. JavaScript: Performance and Efficiency ConsiderationsApr 30, 2025 am 12:08 AM

The differences in performance and efficiency between Python and JavaScript are mainly reflected in: 1) As an interpreted language, Python runs slowly but has high development efficiency and is suitable for rapid prototype development; 2) JavaScript is limited to single thread in the browser, but multi-threading and asynchronous I/O can be used to improve performance in Node.js, and both have advantages in actual projects.

The Origins of JavaScript: Exploring Its Implementation LanguageThe Origins of JavaScript: Exploring Its Implementation LanguageApr 29, 2025 am 12:51 AM

JavaScript originated in 1995 and was created by Brandon Ike, and realized the language into C. 1.C language provides high performance and system-level programming capabilities for JavaScript. 2. JavaScript's memory management and performance optimization rely on C language. 3. The cross-platform feature of C language helps JavaScript run efficiently on different operating systems.

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

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

Hot Tools

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools