63 lines
30 KiB
HTML
63 lines
30 KiB
HTML
<!doctype html>
|
|
<html lang="zh-CN" data-theme="light">
|
|
<head>
|
|
<meta charset="utf-8" />
|
|
<meta name="viewport" content="width=device-width,initial-scale=1" />
|
|
<meta name="generator" content="VuePress 2.0.0-rc.24" />
|
|
<meta name="theme" content="VuePress Theme Hope 2.0.0-rc.94" />
|
|
<style>
|
|
:root {
|
|
--vp-c-bg: #fff;
|
|
}
|
|
|
|
[data-theme="dark"] {
|
|
--vp-c-bg: #1b1b1f;
|
|
}
|
|
|
|
html,
|
|
body {
|
|
background: var(--vp-c-bg);
|
|
}
|
|
</style>
|
|
<script>
|
|
const userMode = localStorage.getItem("vuepress-theme-hope-scheme");
|
|
const systemDarkMode =
|
|
window.matchMedia &&
|
|
window.matchMedia("(prefers-color-scheme: dark)").matches;
|
|
|
|
if (userMode === "dark" || (userMode !== "light" && systemDarkMode)) {
|
|
document.documentElement.setAttribute("data-theme", "dark");
|
|
}
|
|
</script>
|
|
<script type="application/ld+json">{"@context":"https://schema.org","@type":"Article","headline":"对象和类","image":[""],"dateModified":"2025-02-22T14:42:02.000Z","author":[{"@type":"Person","name":"JinChao","url":"https://thecodeway.com"}]}</script><meta property="og:url" content="https://thejinchao.github.io/note/language/javascript/object_and_class.html"><meta property="og:site_name" content="我的博客和笔记"><meta property="og:title" content="对象和类"><meta property="og:description" content="对象和类 对象 JavaScript中的对象(object)用于存储各种键值集合和更复杂的实体。例如下面的对象 如何使用对象 类 javascript中的类(class)是用于创建对象的模板,和c++中的类相似,但是需要在constructor()函数中声明类成员 使用方法"><meta property="og:type" content="article"><meta property="og:locale" content="zh-CN"><meta property="og:updated_time" content="2025-02-22T14:42:02.000Z"><meta property="article:modified_time" content="2025-02-22T14:42:02.000Z"><title>对象和类 | 我的博客和笔记</title><meta name="description" content="对象和类 对象 JavaScript中的对象(object)用于存储各种键值集合和更复杂的实体。例如下面的对象 如何使用对象 类 javascript中的类(class)是用于创建对象的模板,和c++中的类相似,但是需要在constructor()函数中声明类成员 使用方法">
|
|
<link rel="preload" href="/assets/style-DeGCDZm0.css" as="style"><link rel="stylesheet" href="/assets/style-DeGCDZm0.css">
|
|
<link rel="modulepreload" href="/assets/app-DlommLbD.js"><link rel="modulepreload" href="/assets/object_and_class.html-C8Sq9bvd.js"><link rel="modulepreload" href="/assets/plugin-vue_export-helper-DlAUqK2U.js">
|
|
<link rel="prefetch" href="/assets/about.html-BnEMLcqZ.js" as="script"><link rel="prefetch" href="/assets/index.html-jPVMq3lp.js" as="script"><link rel="prefetch" href="/assets/index.html-gMM-Tcqk.js" as="script"><link rel="prefetch" href="/assets/index.html-BP5ZChLS.js" as="script"><link rel="prefetch" href="/assets/flip_coin.html-B2WEW5Us.js" as="script"><link rel="prefetch" href="/assets/index.html-DjkAC4H8.js" as="script"><link rel="prefetch" href="/assets/mental_poker.html-NukViYhN.js" as="script"><link rel="prefetch" href="/assets/rsa.html-DkW71JZH.js" as="script"><link rel="prefetch" href="/assets/index.html-BOW7o10m.js" as="script"><link rel="prefetch" href="/assets/index.html-IbfWY_do.js" as="script"><link rel="prefetch" href="/assets/group.html-CpuVx9QA.js" as="script"><link rel="prefetch" href="/assets/index.html-BoggU2W0.js" as="script"><link rel="prefetch" href="/assets/linear_algebra.html-D2rwkcWr.js" as="script"><link rel="prefetch" href="/assets/number_theory_1.html-DxR9wxh5.js" as="script"><link rel="prefetch" href="/assets/number_theory_2.html-D_7QOhMm.js" as="script"><link rel="prefetch" href="/assets/number_theory_3.html-DlPu_try.js" as="script"><link rel="prefetch" href="/assets/probability.html-CcUq30wr.js" as="script"><link rel="prefetch" href="/assets/symbol.html-B89a6TZU.js" as="script"><link rel="prefetch" href="/assets/DH.html-XHTT2Dni.js" as="script"><link rel="prefetch" href="/assets/Ellipse.html-jPnF-eHK.js" as="script"><link rel="prefetch" href="/assets/Fibonacci.html-Dfag1Tc5.js" as="script"><link rel="prefetch" href="/assets/JPEG001.html-tWNO9VdD.js" as="script"><link rel="prefetch" href="/assets/JPEG002.html-BEmu0vf2.js" as="script"><link rel="prefetch" href="/assets/JPEG003.html-D9Ortpcj.js" as="script"><link rel="prefetch" href="/assets/JPEG004.html-DcmzbaxE.js" as="script"><link rel="prefetch" href="/assets/JPEG005.html-CrflKMjW.js" as="script"><link rel="prefetch" href="/assets/Martingle.html-CrN7W3v6.js" as="script"><link rel="prefetch" href="/assets/MentalPoker01.html-DQgCJzB7.js" as="script"><link rel="prefetch" href="/assets/MentalPoker02.html-COcBjpS-.js" as="script"><link rel="prefetch" href="/assets/RandRound.html-C2MbgskP.js" as="script"><link rel="prefetch" href="/assets/SPH001.html-D2MTcfUh.js" as="script"><link rel="prefetch" href="/assets/SPH002.html-zxw79sTo.js" as="script"><link rel="prefetch" href="/assets/SPH003.html-_EEngzPo.js" as="script"><link rel="prefetch" href="/assets/SPH004.html-Cl8MDUJ-.js" as="script"><link rel="prefetch" href="/assets/SegmentCircle.html-BmcqcBYE.js" as="script"><link rel="prefetch" href="/assets/BezierLine01.html-CCv3Q06c.js" as="script"><link rel="prefetch" href="/assets/BezierLine02.html-je2Sr2gz.js" as="script"><link rel="prefetch" href="/assets/index.html-wTPaBKgF.js" as="script"><link rel="prefetch" href="/assets/ibl_01.html-9-g3WOr_.js" as="script"><link rel="prefetch" href="/assets/ibl_02.html-B9w0S9ZM.js" as="script"><link rel="prefetch" href="/assets/index.html-DvOuOIj1.js" as="script"><link rel="prefetch" href="/assets/index.html-CtAhYpjx.js" as="script"><link rel="prefetch" href="/assets/math_01.html-BiHpFlvW.js" as="script"><link rel="prefetch" href="/assets/math_02.html-DU7nOJT4.js" as="script"><link rel="prefetch" href="/assets/math_03.html-DPY6uVlX.js" as="script"><link rel="prefetch" href="/assets/transform_01.html-BWc2obRV.js" as="script"><link rel="prefetch" href="/assets/transform_02.html-BBme5AL7.js" as="script"><link rel="prefetch" href="/assets/transform_03.html-0Spljc9Z.js" as="script"><link rel="prefetch" href="/assets/transform_04.html-B91CMNsE.js" as="script"><link rel="prefetch" href="/assets/index.html-Xf2P60os.js" as="script"><link rel="prefetch" href="/assets/raster01.html-CiDi8IIr.js" as="script"><link rel="prefetch" href="/assets/raster02.html-DaZmjPFr.js" as="script"><link rel="prefetch" href="/assets/raster03.html-CZ6-ezPj.js" as="script"><link rel="prefetch" href="/assets/function.html-BPDZyrA7.js" as="script"><link rel="prefetch" href="/assets/grammar.html-JXO2EZb3.js" as="script"><link rel="prefetch" href="/assets/helloworld.html-DXN6yoiB.js" as="script"><link rel="prefetch" href="/assets/index.html-CkI9T8iX.js" as="script"><link rel="prefetch" href="/assets/BRDF.html-BcMwSED8.js" as="script"><link rel="prefetch" href="/assets/illumination_model_01.html-B8okaqnE.js" as="script"><link rel="prefetch" href="/assets/luminosity.html-QezvYI4b.js" as="script"><link rel="prefetch" href="/assets/microfacets_1.html-DtDbnYGH.js" as="script"><link rel="prefetch" href="/assets/microfacets_2.html-C3jhKwje.js" as="script"><link rel="prefetch" href="/assets/microfacets_3.html-M4UREu0c.js" as="script"><link rel="prefetch" href="/assets/render_function_1.html-DfDAw2P3.js" as="script"><link rel="prefetch" href="/assets/404.html-DPwPvEI9.js" as="script"><link rel="prefetch" href="/assets/index.html-DoEtvOh-.js" as="script"><link rel="prefetch" href="/assets/index.html-BnSbgljJ.js" as="script"><link rel="prefetch" href="/assets/index.html-CUvLlIy5.js" as="script"><link rel="prefetch" href="/assets/index.html-B2GzXoS9.js" as="script"><link rel="prefetch" href="/assets/index.html-Xz77KHqc.js" as="script"><link rel="prefetch" href="/assets/index.html-CHHuyI8G.js" as="script"><link rel="prefetch" href="/assets/index.html-DS3csrkE.js" as="script"><link rel="prefetch" href="/assets/index.html-B4J2mk8T.js" as="script"><link rel="prefetch" href="/assets/photoswipe.esm-CKV1Bsxh.js" as="script">
|
|
</head>
|
|
<body>
|
|
<div id="app"><!--[--><!--[--><!--[--><span tabindex="-1"></span><a href="#main-content" class="vp-skip-link sr-only">跳至主要內容</a><!--]--><!--[--><div class="theme-container external-link-icon" vp-container><!--[--><header id="navbar" class="vp-navbar" vp-navbar><div class="vp-navbar-start"><button type="button" class="vp-toggle-sidebar-button" title="Toggle Sidebar"><span class="icon"></span></button><!--[--><a class="route-link vp-brand" href="/" aria-label="带我回家"><img class="vp-nav-logo" src="/favicon.png" alt><!----><span class="vp-site-name hide-in-pad">我的博客和笔记</span></a><!--]--></div><div class="vp-navbar-center"><!--[--><nav class="vp-nav-links"><div class="vp-nav-item hide-in-mobile"><a class="route-link auto-link" href="/" aria-label="主页"><!--[--><iconify-icon class="vp-icon" icon="fa6-solid:house" sizing="height" height="1em"></iconify-icon><!--]-->主页<!----></a></div><div class="vp-nav-item hide-in-mobile"><a class="route-link auto-link" href="/blog/" aria-label="文章"><!--[--><iconify-icon class="vp-icon" icon="fa6-solid:book" sizing="height" height="1em"></iconify-icon><!--]-->文章<!----></a></div><div class="vp-nav-item hide-in-mobile"><div class="vp-dropdown-wrapper"><button type="button" class="vp-dropdown-title" aria-label="开源项目"><!--[--><iconify-icon class="vp-icon" icon="fa6-solid:diagram-project" sizing="height" height="1em"></iconify-icon>开源项目<!--]--><span class="arrow"></span><ul class="vp-dropdown"><li class="vp-dropdown-item"><a class="auto-link external-link" href="https://github.com/thejinchao/turbolink" aria-label="TurboLink" rel="noopener noreferrer" target="_blank"><!---->TurboLink<!----></a></li><li class="vp-dropdown-item"><a class="auto-link external-link" href="https://github.com/thejinchao/TinyEncrypt" aria-label="TinyEncrypt" rel="noopener noreferrer" target="_blank"><!---->TinyEncrypt<!----></a></li><li class="vp-dropdown-item"><a class="auto-link external-link" href="https://github.com/thejinchao/ue5-style-guide" aria-label="UnrealStyleGuide" rel="noopener noreferrer" target="_blank"><!---->UnrealStyleGuide<!----></a></li><li class="vp-dropdown-item"><a class="auto-link external-link" href="https://github.com/thejinchao/AxTrace" aria-label="AxTrace" rel="noopener noreferrer" target="_blank"><!---->AxTrace<!----></a></li><li class="vp-dropdown-item"><a class="auto-link external-link" href="https://github.com/thejinchao/cyclone" aria-label="Cyclone" rel="noopener noreferrer" target="_blank"><!---->Cyclone<!----></a></li></ul></button></div></div><div class="vp-nav-item hide-in-mobile"><div class="vp-dropdown-wrapper"><button type="button" class="vp-dropdown-title" aria-label="个人笔记"><!--[--><iconify-icon class="vp-icon" icon="fa6-solid:lightbulb" sizing="height" height="1em"></iconify-icon>个人笔记<!--]--><span class="arrow"></span><ul class="vp-dropdown"><li class="vp-dropdown-item"><a class="route-link auto-link" href="/note/math/" aria-label="数学相关"><!---->数学相关<!----></a></li><li class="vp-dropdown-item"><a class="route-link auto-link" href="/note/graphics/" aria-label="图形学"><!---->图形学<!----></a></li><li class="vp-dropdown-item"><a class="route-link auto-link" href="/note/cryptography/" aria-label="密码学"><!---->密码学<!----></a></li><li class="vp-dropdown-item"><a class="route-link route-link-active auto-link" href="/note/language/" aria-label="编程语言"><!---->编程语言<!----></a></li></ul></button></div></div><div class="vp-nav-item hide-in-mobile"><a class="route-link auto-link" href="/about.html" aria-label="关于"><!--[--><iconify-icon class="vp-icon" icon="fa6-solid:user-astronaut" sizing="height" height="1em"></iconify-icon><!--]-->关于<!----></a></div></nav><!--]--></div><div class="vp-navbar-end"><!--[--><!----><!----><div class="vp-nav-item hide-in-mobile"><button type="button" class="vp-color-mode-switch" id="color-mode-switch"><svg xmlns="http://www.w3.org/2000/svg" class="icon auto-icon" viewBox="0 0 1024 1024" fill="currentColor" aria-label="auto icon" name="auto" style="display:block;"><path d="M512 992C246.92 992 32 777.08 32 512S246.92 32 512 32s480 214.92 480 480-214.92 480-480 480zm0-840c-198.78 0-360 161.22-360 360 0 198.84 161.22 360 360 360s360-161.16 360-360c0-198.78-161.22-360-360-360zm0 660V212c165.72 0 300 134.34 300 300 0 165.72-134.28 300-300 300z"></path></svg><svg xmlns="http://www.w3.org/2000/svg" class="icon dark-icon" viewBox="0 0 1024 1024" fill="currentColor" aria-label="dark icon" name="dark" style="display:none;"><path d="M524.8 938.667h-4.267a439.893 439.893 0 0 1-313.173-134.4 446.293 446.293 0 0 1-11.093-597.334A432.213 432.213 0 0 1 366.933 90.027a42.667 42.667 0 0 1 45.227 9.386 42.667 42.667 0 0 1 10.24 42.667 358.4 358.4 0 0 0 82.773 375.893 361.387 361.387 0 0 0 376.747 82.774 42.667 42.667 0 0 1 54.187 55.04 433.493 433.493 0 0 1-99.84 154.88 438.613 438.613 0 0 1-311.467 128z"></path></svg><svg xmlns="http://www.w3.org/2000/svg" class="icon light-icon" viewBox="0 0 1024 1024" fill="currentColor" aria-label="light icon" name="light" style="display:none;"><path d="M952 552h-80a40 40 0 0 1 0-80h80a40 40 0 0 1 0 80zM801.88 280.08a41 41 0 0 1-57.96-57.96l57.96-58a41.04 41.04 0 0 1 58 58l-58 57.96zM512 752a240 240 0 1 1 0-480 240 240 0 0 1 0 480zm0-560a40 40 0 0 1-40-40V72a40 40 0 0 1 80 0v80a40 40 0 0 1-40 40zm-289.88 88.08-58-57.96a41.04 41.04 0 0 1 58-58l57.96 58a41 41 0 0 1-57.96 57.96zM192 512a40 40 0 0 1-40 40H72a40 40 0 0 1 0-80h80a40 40 0 0 1 40 40zm30.12 231.92a41 41 0 0 1 57.96 57.96l-57.96 58a41.04 41.04 0 0 1-58-58l58-57.96zM512 832a40 40 0 0 1 40 40v80a40 40 0 0 1-80 0v-80a40 40 0 0 1 40-40zm289.88-88.08 58 57.96a41.04 41.04 0 0 1-58 58l-57.96-58a41 41 0 0 1 57.96-57.96z"></path></svg></button></div><!----><!--]--><button type="button" class="vp-toggle-navbar-button" aria-label="Toggle Navbar" aria-expanded="false" aria-controls="nav-screen"><span><span class="vp-top"></span><span class="vp-middle"></span><span class="vp-bottom"></span></span></button></div></header><!----><!--]--><!----><div class="toggle-sidebar-wrapper"><span class="arrow start"></span></div><aside id="sidebar" class="vp-sidebar" vp-sidebar><!----><ul class="vp-sidebar-links"><li><a class="route-link auto-link vp-sidebar-link" href="/" aria-label="主页"><!--[--><iconify-icon class="vp-icon" icon="fa6-solid:house" sizing="both" width="1em" height="1em"></iconify-icon><!--]-->主页<!----></a></li><li><section class="vp-sidebar-group"><button class="vp-sidebar-header clickable" type="button"><iconify-icon class="vp-icon" icon="fa6-solid:book" sizing="both" width="1em" height="1em"></iconify-icon><span class="vp-sidebar-title">我的文章</span><span class="vp-arrow down"></span></button><ul class="vp-sidebar-links"><li><section class="vp-sidebar-group"><button class="vp-sidebar-header clickable" type="button"><!----><span class="vp-sidebar-title">从抛币协议到智能合约</span><span class="vp-arrow end"></span></button><!----></section></li><li><section class="vp-sidebar-group"><button class="vp-sidebar-header clickable" type="button"><!----><span class="vp-sidebar-title">JPEG算法解密</span><span class="vp-arrow end"></span></button><!----></section></li><li><section class="vp-sidebar-group"><button class="vp-sidebar-header clickable" type="button"><!----><span class="vp-sidebar-title">SPH算法简介</span><span class="vp-arrow end"></span></button><!----></section></li><li><a class="route-link auto-link vp-sidebar-link" href="/blog/2025/02/Martingle.html" aria-label="赌博中的数学:Martingle策略"><!---->赌博中的数学:Martingle策略<!----></a></li><li><a class="route-link auto-link vp-sidebar-link" href="/blog/2025/02/RandRound.html" aria-label="如何生成一个随机的圆形"><!---->如何生成一个随机的圆形<!----></a></li><li><a class="route-link auto-link vp-sidebar-link" href="/blog/2025/02/DH.html" aria-label="一个简单的DH密钥协商算法的实现"><!---->一个简单的DH密钥协商算法的实现<!----></a></li><li><a class="route-link auto-link vp-sidebar-link" href="/blog/2025/02/SegmentCircle.html" aria-label="如何计算线段和圆的交点"><!---->如何计算线段和圆的交点<!----></a></li><li><a class="route-link auto-link vp-sidebar-link" href="/blog/2025/02/Ellipse.html" aria-label="一道数学趣题"><!---->一道数学趣题<!----></a></li><li><a class="route-link auto-link vp-sidebar-link" href="/blog/2025/02/Fibonacci.html" aria-label="斐波那契数列和1/89"><!---->斐波那契数列和1/89<!----></a></li><li><section class="vp-sidebar-group"><button class="vp-sidebar-header clickable" type="button"><!----><span class="vp-sidebar-title">匀速贝塞尔曲线运动的实现</span><span class="vp-arrow end"></span></button><!----></section></li></ul></section></li><li><section class="vp-sidebar-group"><button class="vp-sidebar-header clickable" type="button"><iconify-icon class="vp-icon" icon="fa6-solid:diagram-project" sizing="both" width="1em" height="1em"></iconify-icon><span class="vp-sidebar-title">开源项目</span><span class="vp-arrow end"></span></button><!----></section></li><li><section class="vp-sidebar-group"><button class="vp-sidebar-header clickable active" type="button"><iconify-icon class="vp-icon" icon="fa6-solid:lightbulb" sizing="both" width="1em" height="1em"></iconify-icon><span class="vp-sidebar-title">学习笔记</span><span class="vp-arrow down"></span></button><ul class="vp-sidebar-links"><li><section class="vp-sidebar-group"><button class="vp-sidebar-header clickable" type="button"><!----><span class="vp-sidebar-title">数学相关</span><span class="vp-arrow end"></span></button><!----></section></li><li><section class="vp-sidebar-group"><button class="vp-sidebar-header clickable" type="button"><!----><span class="vp-sidebar-title">密码学</span><span class="vp-arrow end"></span></button><!----></section></li><li><section class="vp-sidebar-group"><button class="vp-sidebar-header clickable" type="button"><!----><span class="vp-sidebar-title">图形学</span><span class="vp-arrow end"></span></button><!----></section></li><li><section class="vp-sidebar-group"><button class="vp-sidebar-header clickable active" type="button"><!----><span class="vp-sidebar-title">编程语言</span><span class="vp-arrow down"></span></button><ul class="vp-sidebar-links"><li><section class="vp-sidebar-group"><button class="vp-sidebar-header clickable active" type="button"><!----><span class="vp-sidebar-title">JavaScript</span><span class="vp-arrow down"></span></button><ul class="vp-sidebar-links"><li><a class="route-link auto-link vp-sidebar-link" href="/note/language/javascript/helloworld.html" aria-label="环境搭建"><!---->环境搭建<!----></a></li><li><a class="route-link auto-link vp-sidebar-link" href="/note/language/javascript/grammar.html" aria-label="基本语法"><!---->基本语法<!----></a></li><li><a class="route-link auto-link vp-sidebar-link" href="/note/language/javascript/function.html" aria-label="函数"><!---->函数<!----></a></li><li><a class="route-link route-link-active auto-link vp-sidebar-link active" href="/note/language/javascript/object_and_class.html" aria-label="对象和类"><!---->对象和类<!----></a></li></ul></section></li></ul></section></li></ul></section></li></ul><!----></aside><!--[--><main id="main-content" class="vp-page"><!--[--><!----><!----><nav class="vp-breadcrumb disable"></nav><div class="vp-page-title"><h1><!---->对象和类</h1><!----><hr></div><!----><div class="" vp-content><!----><div id="markdown-content"><h1 id="对象和类" tabindex="-1"><a class="header-anchor" href="#对象和类"><span>对象和类</span></a></h1><h2 id="对象" tabindex="-1"><a class="header-anchor" href="#对象"><span>对象</span></a></h2><p>JavaScript中的对象(object)用于存储各种键值集合和更复杂的实体。例如下面的对象</p><div class="language-javascript" data-highlighter="shiki" data-ext="javascript" style="--shiki-light:#383A42;--shiki-dark:#abb2bf;--shiki-light-bg:#FAFAFA;--shiki-dark-bg:#282c34;"><pre class="shiki shiki-themes one-light one-dark-pro vp-code"><code class="language-javascript"><span class="line"><span style="--shiki-light:#A626A4;--shiki-dark:#C678DD;">var</span><span style="--shiki-light:#383A42;--shiki-dark:#E06C75;"> person</span><span style="--shiki-light:#0184BC;--shiki-dark:#56B6C2;"> =</span><span style="--shiki-light:#383A42;--shiki-dark:#ABB2BF;"> {</span></span>
|
|
<span class="line"><span style="--shiki-light:#E45649;--shiki-dark:#E06C75;"> firstName</span><span style="--shiki-light:#0184BC;--shiki-dark:#ABB2BF;">:</span><span style="--shiki-light:#50A14F;--shiki-dark:#98C379;"> 'John'</span><span style="--shiki-light:#383A42;--shiki-dark:#ABB2BF;">,</span></span>
|
|
<span class="line"><span style="--shiki-light:#E45649;--shiki-dark:#E06C75;"> lastName</span><span style="--shiki-light:#0184BC;--shiki-dark:#ABB2BF;">:</span><span style="--shiki-light:#50A14F;--shiki-dark:#98C379;"> 'Doe'</span><span style="--shiki-light:#383A42;--shiki-dark:#ABB2BF;">,</span></span>
|
|
<span class="line"><span style="--shiki-light:#E45649;--shiki-dark:#E06C75;"> age</span><span style="--shiki-light:#0184BC;--shiki-dark:#ABB2BF;">:</span><span style="--shiki-light:#986801;--shiki-dark:#D19A66;"> 30</span><span style="--shiki-light:#383A42;--shiki-dark:#ABB2BF;">,</span></span>
|
|
<span class="line"><span style="--shiki-light:#E45649;--shiki-dark:#E06C75;"> isStudent</span><span style="--shiki-light:#0184BC;--shiki-dark:#ABB2BF;">:</span><span style="--shiki-light:#986801;--shiki-dark:#D19A66;"> false</span><span style="--shiki-light:#383A42;--shiki-dark:#ABB2BF;">,</span></span>
|
|
<span class="line"><span style="--shiki-light:#4078F2;--shiki-dark:#61AFEF;"> greet</span><span style="--shiki-light:#0184BC;--shiki-dark:#ABB2BF;">:</span><span style="--shiki-light:#A626A4;--shiki-dark:#C678DD;"> function</span><span style="--shiki-light:#383A42;--shiki-dark:#ABB2BF;">() {</span></span>
|
|
<span class="line"><span style="--shiki-light:#383A42;--shiki-dark:#E5C07B;"> console</span><span style="--shiki-light:#383A42;--shiki-dark:#ABB2BF;">.</span><span style="--shiki-light:#4078F2;--shiki-dark:#61AFEF;">log</span><span style="--shiki-light:#383A42;--shiki-dark:#ABB2BF;">(</span><span style="--shiki-light:#50A14F;--shiki-dark:#98C379;">'Hello, I am '</span><span style="--shiki-light:#0184BC;--shiki-dark:#56B6C2;"> +</span><span style="--shiki-light:#E45649;--shiki-dark:#E5C07B;"> this</span><span style="--shiki-light:#383A42;--shiki-dark:#ABB2BF;">.</span><span style="--shiki-light:#E45649;--shiki-dark:#E06C75;">firstName</span><span style="--shiki-light:#0184BC;--shiki-dark:#56B6C2;"> +</span><span style="--shiki-light:#50A14F;--shiki-dark:#98C379;"> ' '</span><span style="--shiki-light:#0184BC;--shiki-dark:#56B6C2;"> +</span><span style="--shiki-light:#E45649;--shiki-dark:#E5C07B;"> this</span><span style="--shiki-light:#383A42;--shiki-dark:#ABB2BF;">.</span><span style="--shiki-light:#E45649;--shiki-dark:#E06C75;">lastName</span><span style="--shiki-light:#383A42;--shiki-dark:#ABB2BF;">);</span></span>
|
|
<span class="line"><span style="--shiki-light:#383A42;--shiki-dark:#ABB2BF;"> }</span></span>
|
|
<span class="line"><span style="--shiki-light:#4078F2;--shiki-dark:#61AFEF;"> print_name</span><span style="--shiki-light:#383A42;--shiki-dark:#ABB2BF;">() {</span></span>
|
|
<span class="line"><span style="--shiki-light:#383A42;--shiki-dark:#ABB2BF;"> console.log(lastName + </span><span style="--shiki-light:#50A14F;--shiki-dark:#98C379;">' '</span><span style="--shiki-light:#E45649;--shiki-dark:#E06C75;"> + firstName) </span></span>
|
|
<span class="line"><span style="--shiki-light:#E45649;--shiki-dark:#E06C75;"> }</span></span>
|
|
<span class="line"><span style="--shiki-light:#E45649;--shiki-dark:#E06C75;">};</span></span></code></pre></div><p>如何使用对象</p><div class="language-javascript" data-highlighter="shiki" data-ext="javascript" style="--shiki-light:#383A42;--shiki-dark:#abb2bf;--shiki-light-bg:#FAFAFA;--shiki-dark-bg:#282c34;"><pre class="shiki shiki-themes one-light one-dark-pro vp-code"><code class="language-javascript"><span class="line"><span style="--shiki-light:#383A42;--shiki-dark:#E5C07B;">console</span><span style="--shiki-light:#383A42;--shiki-dark:#ABB2BF;">.</span><span style="--shiki-light:#4078F2;--shiki-dark:#61AFEF;">log</span><span style="--shiki-light:#383A42;--shiki-dark:#ABB2BF;">(</span><span style="--shiki-light:#383A42;--shiki-dark:#E5C07B;">person</span><span style="--shiki-light:#383A42;--shiki-dark:#ABB2BF;">.</span><span style="--shiki-light:#E45649;--shiki-dark:#E06C75;">firstName</span><span style="--shiki-light:#383A42;--shiki-dark:#ABB2BF;">); </span><span style="--shiki-light:#A0A1A7;--shiki-light-font-style:italic;--shiki-dark:#7F848E;--shiki-dark-font-style:italic;">// 输出: John</span></span>
|
|
<span class="line"><span style="--shiki-light:#383A42;--shiki-dark:#E5C07B;">person</span><span style="--shiki-light:#383A42;--shiki-dark:#ABB2BF;">.</span><span style="--shiki-light:#4078F2;--shiki-dark:#61AFEF;">greet</span><span style="--shiki-light:#383A42;--shiki-dark:#ABB2BF;">(); </span><span style="--shiki-light:#A0A1A7;--shiki-light-font-style:italic;--shiki-dark:#7F848E;--shiki-dark-font-style:italic;">// 输出: Hello, I am John Doe</span></span></code></pre></div><h2 id="类" tabindex="-1"><a class="header-anchor" href="#类"><span>类</span></a></h2><p>javascript中的类(class)是用于创建对象的模板,和c++中的类相似,但是需要在<code>constructor()</code>函数中声明类成员</p><div class="language-javascript" data-highlighter="shiki" data-ext="javascript" style="--shiki-light:#383A42;--shiki-dark:#abb2bf;--shiki-light-bg:#FAFAFA;--shiki-dark-bg:#282c34;"><pre class="shiki shiki-themes one-light one-dark-pro vp-code"><code class="language-javascript"><span class="line"><span style="--shiki-light:#A626A4;--shiki-dark:#C678DD;">class</span><span style="--shiki-light:#C18401;--shiki-dark:#E5C07B;"> User</span><span style="--shiki-light:#383A42;--shiki-dark:#ABB2BF;"> {</span></span>
|
|
<span class="line"><span style="--shiki-light:#A626A4;--shiki-dark:#C678DD;"> constructor</span><span style="--shiki-light:#383A42;--shiki-dark:#ABB2BF;">(</span><span style="--shiki-light:#383A42;--shiki-light-font-style:inherit;--shiki-dark:#E06C75;--shiki-dark-font-style:italic;">name</span><span style="--shiki-light:#383A42;--shiki-dark:#ABB2BF;">) {</span></span>
|
|
<span class="line"><span style="--shiki-light:#E45649;--shiki-dark:#E5C07B;"> this</span><span style="--shiki-light:#383A42;--shiki-dark:#ABB2BF;">.</span><span style="--shiki-light:#E45649;--shiki-dark:#E06C75;">name</span><span style="--shiki-light:#0184BC;--shiki-dark:#56B6C2;"> =</span><span style="--shiki-light:#383A42;--shiki-dark:#E06C75;"> name</span><span style="--shiki-light:#383A42;--shiki-dark:#ABB2BF;">;</span></span>
|
|
<span class="line"><span style="--shiki-light:#383A42;--shiki-dark:#ABB2BF;"> }</span></span>
|
|
<span class="line"></span>
|
|
<span class="line"><span style="--shiki-light:#4078F2;--shiki-dark:#61AFEF;"> greet</span><span style="--shiki-light:#383A42;--shiki-dark:#ABB2BF;">() {</span></span>
|
|
<span class="line"><span style="--shiki-light:#383A42;--shiki-dark:#E5C07B;"> console</span><span style="--shiki-light:#383A42;--shiki-dark:#ABB2BF;">.</span><span style="--shiki-light:#4078F2;--shiki-dark:#61AFEF;">log</span><span style="--shiki-light:#383A42;--shiki-dark:#ABB2BF;">(</span><span style="--shiki-light:#50A14F;--shiki-dark:#98C379;">"Hello, I'm "</span><span style="--shiki-light:#0184BC;--shiki-dark:#56B6C2;"> +</span><span style="--shiki-light:#E45649;--shiki-dark:#E5C07B;"> this</span><span style="--shiki-light:#383A42;--shiki-dark:#ABB2BF;">.</span><span style="--shiki-light:#E45649;--shiki-dark:#E06C75;">name</span><span style="--shiki-light:#383A42;--shiki-dark:#ABB2BF;">) </span></span>
|
|
<span class="line"><span style="--shiki-light:#383A42;--shiki-dark:#ABB2BF;"> }</span></span>
|
|
<span class="line"><span style="--shiki-light:#383A42;--shiki-dark:#ABB2BF;">}</span></span></code></pre></div><p>使用方法</p><div class="language-javascript" data-highlighter="shiki" data-ext="javascript" style="--shiki-light:#383A42;--shiki-dark:#abb2bf;--shiki-light-bg:#FAFAFA;--shiki-dark-bg:#282c34;"><pre class="shiki shiki-themes one-light one-dark-pro vp-code"><code class="language-javascript"><span class="line"><span style="--shiki-light:#A626A4;--shiki-dark:#C678DD;">var</span><span style="--shiki-light:#383A42;--shiki-dark:#E06C75;"> user</span><span style="--shiki-light:#0184BC;--shiki-dark:#56B6C2;"> =</span><span style="--shiki-light:#A626A4;--shiki-dark:#C678DD;"> new</span><span style="--shiki-light:#4078F2;--shiki-dark:#61AFEF;"> User</span><span style="--shiki-light:#383A42;--shiki-dark:#ABB2BF;">(</span><span style="--shiki-light:#50A14F;--shiki-dark:#98C379;">"Alice"</span><span style="--shiki-light:#383A42;--shiki-dark:#ABB2BF;">)</span></span>
|
|
<span class="line"><span style="--shiki-light:#383A42;--shiki-dark:#E5C07B;">user</span><span style="--shiki-light:#383A42;--shiki-dark:#ABB2BF;">.</span><span style="--shiki-light:#4078F2;--shiki-dark:#61AFEF;">greet</span><span style="--shiki-light:#383A42;--shiki-dark:#ABB2BF;">()</span></span></code></pre></div></div><!----><!----><!----></div><footer class="vp-page-meta"><!----><div class="vp-meta-item git-info"><!----><!----></div></footer><!----><!----><!----><!--]--></main><!--]--><footer class="vp-footer-wrapper" vp-footer><div class="vp-footer">使用 <a href="https://theme-hope.vuejs.press/zh/" target="_blank">VuePress Theme Hope</a> 主题 | <a href="https://beian.miit.gov.cn/" target="_blank">京ICP备16018986号-1</a></div><!----></footer></div><!--]--><!--]--><!--[--><!----><!--[--><!--]--><!--]--><!--]--></div>
|
|
<script type="module" src="/assets/app-DlommLbD.js" defer></script>
|
|
</body>
|
|
</html>
|