首页 > 其他分享 >JS constructors

JS constructors

时间:2023-05-25 15:35:21浏览次数:42  
标签:object name constructors JS introduceSelf Hi constructor new

我们可以这样create一个object:

const person1 = {
  name: "Chris",
  introduceSelf() {
    console.log(`Hi! I'm ${this.name}.`);
  },
};

但是当我们需要创建多个对象的时候,每次都要重复同样的code,这时候我们可以用构造函数

A better way is to use a constructor. A constructor is just a function called using the new keyword. When you call a constructor, it will:

  • create a new object
  • bind this to the new object, so you can refer to this in your constructor code
  • run the code in the constructor
  • return the new object.

Constructors, by convention, start with a capital letter and are named for the type of object they create. So we could rewrite our example like this:

function Person(name) {
  this.name = name;
  this.introduceSelf = function () {
    console.log(`Hi! I'm ${this.name}.`);
  };
}

To call Person() as a constructor, we use new:

const salva = new Person("Salva");
salva.name;
salva.introduceSelf();
// "Hi! I'm Salva."

const frankie = new Person("Frankie");
frankie.name;
frankie.introduceSelf();
// "Hi! I'm Frankie."

 

标签:object,name,constructors,JS,introduceSelf,Hi,constructor,new
From: https://www.cnblogs.com/saaspeter/p/17431420.html

相关文章

  • 基于Qt的音乐播放器(三)通过酷狗音乐的api接口,返回json格式歌曲信息(播放地址,歌词,图片)
    2020博客之星年度总评选进行中:请为74号的狗子投上宝贵的一票!我的投票地址:点击为我投票文章目录前言1.获取歌曲搜索列表api接口2.获取单个歌曲详细信息包括歌词3.总结前言首先说明,本教程仅供个人学习,研究使用,禁止用于任何的商业和非法用途。(手动狗头)之所以要研究这个,是因为我想......
  • Oracle中读取JSON格式数据实战指南(oracle中读json)
    Oracle中读取JSON格式数据实战指南 随着大数据、云计算等技术的快速发展,JSON(JavaScriptObjectNotation)格式的数据越来越广泛应用于数据交互和存储中。Oracle数据库支持JSON格式数据的存储和查询,本篇文章将介绍如何在Oracle中读取JSON格式数据,并提供相关代码示例。 1.创建......
  • js \x 反斜杠x 16进制 编解码
    解码functiondecode(str){ returnstr.replace(/\\x(\w{2})/g,function(_,$1){returnString.fromCharCode(parseInt($1,16))});}编码functionencode(str){returnstr.replace(/(\w)/g,function(_,$1){return"\\x"+$1.charCodeAt(0).toString(16)......
  • JS 树形数据 Tree的通用方法
    点击查看代码/***@description查找包含自身节点的父代节点*@paramlist列表数据*@paramid节点id*@parampid节点的父id*/exportfunctionlistToTree(list,id,pid){list.forEach((node)=>{constpNdoe=list.find((row)=>row[id]===nod......
  • jquery/js 根据下拉框选择的值进行按钮展示
    学习如逆水行舟,不进则退~最近接了一个n年前的老项目增加新功能,本以为手到擒来结果再写页面上的效果时还是翻车了,特此记录一下~ 将近三四年没写jsp的页面了,基本忘光了,现在是要做一个批量下载的功能,但是又要是特定的某一个才有这个功能所以就有了以下的过程。一、实现效果......
  • js基础之Promise详解
    1.是什么Promise是一种异步编程的解决方案,用于处理异步操作并返回结果。主要作用是解决回调函数嵌套(回调地狱)的问题,使异步操作更加清晰、易于理解和维护。2.怎么用Promise有三种状态:pending(进行中)、fulfilled(已成功)和rejected(已失败)。当一个Promise被创建时,它的状态为pendin......
  • jsdom, proxy对象(补环境神器)以及抠代码总结的问题
    jsdomconstjsdom=require('jsdom')const{JSDOM}=jsdomconstfs=require('fs')options={url:'http://match.yuanrenxue.com/match/2',referrer:'http://match.yuansrenxue.com/match/2',......
  • 时间不等人,但 Moment.js 可以等你解决时间问题!
    前言一直以来,处理时间和日期的JavaScript库,选用的都是Moment.js。它的API清晰简单,使用方便灵巧,功能还特别齐全。我是Moment.js的重度使用者。凡是遇到时间和日期的操作,就把Moment.js引用上。简介Moment.js是一款常用于JavaScript日期时间处理的代码库,它能够解析、验证、操作以......
  • 01-Node.js介绍
    01.Node.js是什么?pNode.js是一个基于V8JavaScript引擎的JavaScript运行时环境。也就是说:Node.js基于V8引擎来执行JavaScript的代码。V8引擎可以嵌入到任何C++应用程序中,无论是Chrome还是Node.js,事实上都是嵌入了V8引擎来执行JavaScript代码的。但需要注意的是:两者都不仅......
  • 02-Node.js的包管理工具
    00.代码共享方案模块化的编程思想,支持将代码划分成一个个小的、独立的结构。我们可以通过模块化的方式来封装自己的代码,将之封装成一个工具;这个工具我们可以让同事通过导入的方式来使用,甚至也可以分享给世界各地的程序员来使用;假如,我们要将某个工具分享给世界上所有的程序员......