Original artial --> link

How descorator looks like:

@mydecorator
function myFun(){
...
}

Descorator in action:

We have a class, which have an method call meow():

class Cat {
meow(){
console.log(`Meow!!`);
}
}

When Javascritp Engine read it, it looks like:

Object.defineProperty(Cat.prototype, 'meow', {
value: specifiedFunction,
enumerable: false,
configurable: true,
writable: true,
})

Imagine we want to mark a property or method name as not being writable. A decorator precedes the syntax that defines a property. We could thus define a`@readonly` decorator for it as follows:

function readonly(target, key, descriptor){
descriptor.writable = false;
return descriptor;
}

and add it to our meow property as follows:

class Cat {
@readonly
meow() {
return `say meow!`
}
}

Now, before installing the descriptor onto `Cat.prototype`, the engine first invokes the decorator:

Then if you have mark 'writable' to 'false' by adding  @readonly descortaor,  if we trying to overwrite the 'meow' method, it will report error.

Check out libaray, it has many pre-defined descorators: npm, Github

Decorate a class:

function superhero(isSuperhero ){
return function(target){
return class{
isSuperhero = isSuperhero
}
}
} @superhero(true)
class MySuperHero{
isSuperhero = null;
constructor(){}
}
const hero = new MySuperHero();
console.log(hero.isSuperhero); //true

Continue reading:

[ES7] Exploring ES2016 Decorators的更多相关文章

  1. 学习笔记: ES7(ES2016)新功能

    ES7添加了两个新功能 : 1. Array.prototype.includes() 2. 指数运算符 1 .Array.prototype,includes() 判断指定的元素是否存在于数组中,  ...

  2. ES5, ES6, ES2016, ES.Next: JavaScript 的版本是怎么回事?

    原网址:http://huangxuan.me/2015/09/22/js-version/ JavaScript 有着很奇怪的命名史. 1995 年,它作为网景浏览器(Netscape Naviga ...

  3. 10分钟学会ES7+ES8

    撰文为何 身为一个前端开发者,ECMAScript(以下简称ES)早已广泛应用在我们的工作当中.了解ECMA机构流程的人应该知道,标准委员会会在每年的6月份正式发布一次规范的修订,而这次的发布也将作为 ...

  4. es7,es8

    ES7新特性 ES7在ES6的基础上添加了三项内容:求幂运算符(**).Array.prototype.includes()方法.函数作用域中严格模式的变更. Array.prototype.incl ...

  5. 学习ES7+ES8

    es6 语法:http://es6.ruanyifeng.com/#docs/async    作者:阮一峰 撰文为何 身为一个前端开发者,ECMAScript(以下简称ES)早已广泛应用在我们的工作 ...

  6. es6/es7/es8常用新特性总结(超实用)

    本文标题有误导性,因为我其实想写node8的新特性,说实话一下子从node v1.x跳跃到node 8.x+ 真有点受宠若惊的感觉.一直觉得node 数组. 对象.序列等的处理没有python方便,因 ...

  7. 001.TypeScript简介.md

    TypeScript是一门开源的,由微软开发维护的,发布于2012年10月的静态类型的语言: 他是ECMAScript的超集,支持JavaScript的所有语法和语义,并且在此基础之上提供了更多额外的 ...

  8. 为什么说Babel将推动JavaScript的发展

    Babel是一个转换编译器,它能将ES6转换成可以在浏览器中运行的代码.Babel由来自澳大利亚的开发者Sebastian McKenzie创建.他的目标是使Babel可以处理ES6的所有新语法,并为 ...

  9. es2017新特性

    2017年6月底es2017不期而至; 截止目前es8是ecmascript规范的第九个版本:自es2015开始ECMA协会将每年发布一个版本并将年号作为版本号:算了 直接看下es2017的新特性: ...

随机推荐

  1. ipad ------ 与iPhone的差别

    1. 差异 iPhone是手机,iPad.iPad Mini是平板电脑 iPhone和iPad开发的区别 屏幕的尺寸 \分辨率 UI元素的排布 \设计 键盘 API 屏幕方向的支持 … … 2. iP ...

  2. 转:Stack Overflow通过关注性能,实现单块应用架构的扩展能力

    原文来自于:http://www.infoq.com/cn/news/2015/07/scaling-stack-overflow 在New York QCon 2015大会上,David Fulle ...

  3. String, StringBuffer, StringBuilder比较

    1.见API: String是不可变的字符序列: StringBuffer是线程安全的,可变的字符序列: StringBuilder是可变的字符序列: StringBuffer与String的区别是S ...

  4. 运维安全系列基础服务之 FTP 服务(系列一)

    做了多年运维工程师,积攒了一些经验,和大家分享下.个人认为,运维安全话题的系列,主要包括下面四个方面: 基础服务 网络层 应用层 云安全 今天主要讲的是基础服务里面的[FTP服务][ftp]. 文件传 ...

  5. Mongodb数据更新命令

    一.Mongodb数据更新命令 Mongodb更新有两个命令:update.save. 1.1update命令 update命令格式: db.collection.update(criteria,ob ...

  6. PAIP.提升效率----论项目知识库的建设。。

    PAIP.提升效率----论项目知识库的建设.. 作者Attilax ,  EMAIL:1466519819@qq.com  来源:attilax的专栏 地址:http://blog.csdn.net ...

  7. 【HDOJ】4183 Pahom on Water

    就是一个网络流.red结点容量为2,查看最大流量是否大于等于2.对于条件2,把边反向加入建图.条件1,边正向加入建图. /* 4183 */ #include <iostream> #in ...

  8. UVA_303_Pipe_(计算几何基础)

    描述 https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=5&page ...

  9. Scala:(3)数组

    要点: (1)长度固定使用Array,长度变化的则使用ArrayBuffer. (2)提供初始值时,不使用new. (3)用()访问元素 val a= new Array[String](10)//初 ...

  10. 如何编译Support7Demos测试appcompat

    目录(?)[-] 简介 在Eclipse中编译 使用Gradle编译 简介 Google发布的API-18中带上的support-v7包含了appcompat组件,可以在小于API-11的androi ...