JavaScript语法糖的简析

news/2024/7/3 10:54:46

前言

学习JS中函数的时候,看到了原型链的内容,不理解,查资料,发现了语法糖的定义.
首先说明一下语法糖的定义百度百科上的定义:
语法糖(Syntactic sugar),也译为糖衣语法,是由英国计算机科学家彼得·约翰·兰达(Peter J. Landin)发明的一个术语,指计算机语言中添加的某种语法,这种语法对语言的功能并没有影响,但是更方便程序员使用。

正文

一 构造函数

构造函数比较容易理解的:
如下的构造函数就是求两个数和的函数.看起来比较简单

//构造函数
function MathHandle(x,y){
    this.x=x;
    this.y=y;
}
MathHandle.prototype.add=function(){
    return this.x+this.y;
}
var m=new MathHandle(1,2);
console.log(m.add())

二 JavaScript中class的基本语法

class MathHandle{
    constructor(x,y){
        this.x=x;
        this.y=y;
    }
    add(){
        return this.x+this.y;
    }
}
const m=new MathHandle(3,2);
console.log(m.add())

三 运行比较

console.log(typeof MathHandle)  //'function'
console.log(MathHandle.prototype.constructor===MathHandle) //true 
//函数都是有显示原型的,prototype就是显示原型,prototype默认有一个constructor属性,这个属性等于MathHandle本身
console.log(m.__proto__===MathHandle.prototype) //true  
//m是构造函数new出来的一个实例,所有的实例都有一个隐式原型__proto__等于MathHandle的显示原型

结论:class就是语法糖,在语法上更贴近面向对象的写法

四 Class继承的写法

class Animal{
    constructor(name){
        this.name=name
    }
    eat(){
        alert(this.name+'eat')
    }
}
class Bird extends Animal{
    constructor(name){
        super(name)//注意!!!!!
        this.name=name;
    }
    say(){
        alert(this.name + 'say')
    }
}
const bird=new Bird('Pengguin')
bird.say()
bird.eat()

五 继承JS的写法

//动物
function Animal(){
    this.eat=function(){
        alert('eat')
    }
}
//鸟
function Bird(){
    this.bark=function(){
        alert('bark')
    }
}
//绑定原型,实现继承
Bird.prototype=new Animal()

//企鹅
var Pengguin=new Bird()

Pengguin .bark()
Pengguin.eat()

结论:通过语法糖可增加代码的可读性.

结尾

不积硅步无以至千里,不积小流无以成江河.学习更是这样,点点滴滴的学与总结.


http://www.niftyadmin.cn/n/1974605.html

相关文章

Shell Step by Step (3) —— Stdin amp; if

4.输入输出 #! /bin/bash # Read users input and then get his name read -p "Please input your first name: " firstName read -p "Please input your last name: " lastName echo -e "Your full name is: $firstName $lastName" read使用…

nginx搭建文件服务器

nginx搭建文件服务器 安装: sudo apt-get install nginx 启动: nginx -c /etc/nginx/nginx.conf 或: /etc/init.d/nginx start 创建server文件夹,将server.conf文件放在此文件夹中: sudo mkdir/etc/nginx/server ser…

JavaScropt函数

前言 JavaScript 学习的也有一段时间了,好多东西都没有写出来,所以开始将一些简单的知识点总结一下慢慢的积累沉淀吧! 正文 JavaScript函数声明语法: function functionName(parameters){ 需要执行的代码 }Function()构造函数 函数同样可以通过内置的JavaScript 函数构造器…

Aizu 2450 Do use segment tree 树链剖分+线段树

Do use segment tree Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://www.bnuoj.com/v3/problem_show.php?pid39566Description Given a tree with n (1 ≤ n ≤ 200,000) nodes and a list of q (1 ≤ q ≤ 100,000) queries, process the queries in order and …

centos7安装k8s部署系统

centos7安装k8s部署系统 一、环境准备 标题1、设置唯一的静态ip vi /etc/sysconfig/network-scripts/ifcfg-ens33将 BOOTPROTO 改为static BOOTPROTOstatic ONBOOTyes 添加ip、网关和DNS地址,网关可以通过命令:“netstat -rn” 查看 IPADDR192.168.2…

Hibernate 5.0.2加载hibernate.cfg.xml时mapping不生效

2019独角兽企业重金招聘Python工程师标准>>> //Group类 package com.jingtai;public class Group {private int groupId;private String groupName;public void setGroupId(int id){groupId id;}public int getGroupId(){return groupId;}public void setGroupName…

k8s运行minio的yml文件

k8s运行minio的yml文件 apiVersion: apps/v1 kind: Deployment metadata:labels:app: minioname: minionamespace: default spec:replicas: 1selector:matchLabels:app: miniotemplate:metadata:labels:app: miniospec:containers:- args:- server- /data- --console-address-…

端口详解2

5050|多媒体会议控制协议5051|ITA代理5052|ITA管理5137|MyCTS服务器端口5150|Ascend通道管理协议5154|BZFlag游戏服务器5190|America-Online(美国在线)5191|AmericaOnline1(美国在线)5192|AmericaOnline2(美国在线)5193|AmericaOnline3(美国在线)5222|Jabber客户端连接5225|HP(…