剩余参数(rest arguments) Mixin
Mixin – Pug 中文文档 https://pug.bootcss.com/language/mixins.html
混入 Mixin
混入是一种允许您在 Pug 中重复使用一整个代码块的方法。
//- 定义
mixin list
ul
li foo
li bar
li baz
//- 使用
+list
+list
<ul>
<li>foo</li>
<li>bar</li>
<li>baz</li>
</ul>
<ul>
<li>foo</li>
<li>bar</li>
<li>baz</li>
</ul>
它们会被编译成函数形式,您可以传递一些参数:
mixin pet(name)
li.pet= name
ul
+pet('猫')
+pet('狗')
+pet('猪')
<ul>
<li class="pet">猫</li>
<li class="pet">狗</li>
<li class="pet">猪</li>
</ul>
混入的块
混入也可以把一整个代码块像内容一样传递进来:
mixin article(title)
.article
.article-wrapper
h1= title
if block
block
else
p 没有提供任何内容。
+article('Hello world')
+article('Hello world')
p 这是我
p 随便写的文章
<div class="article">
<div class="article-wrapper">
<h1>Hello world</h1>
<p>没有提供任何内容。</p>
</div>
</div>
<div class="article">
<div class="article-wrapper">
<h1>Hello world</h1>
<p>这是我</p>
<p>随便写的文章</p>
</div>
</div>
混入的属性
mixin link(href, name)
//- attributes == {class: "btn"}
a(class!=attributes.class href=href)= name
+link('/foo', 'foo')(class="btn")
<a class="btn" href="/foo">foo</a>
提示
attributes 里的值已经被(作为标签属性)转义了,所以您可能需要用 != 的方式赋值以避免发生二次转义(详细解释可以查阅不转义的属性)。
您也可以直接用 &attributes 方法来传递 attributes 参数:
mixin link(href, name)
a(href=href)&attributes(attributes)= name
+link('/foo', 'foo')(class="btn")
<a class="btn" href="/foo">foo</a>
提示
+link(class="btn") 这种写法也是允许的,且等价于 +link()(class="btn"),因为 Pug 会判断括号内的内容是属性还是参数。但我们鼓励您使用后者的写法,明确地传递空的参数,确保第一对括号内始终都是参数列表。
剩余参数
您可以用剩余参数(rest arguments)语法来表示参数列表最后传入若干个长度不定的参数,比如:
mixin list(id, ...items)
ul(id=id)
each item in items
li= item
+list('my-list', 1, 2, 3, 4)
<ul id="my-list">
<li>1</li>
<li>2</li>
<li>3</li>
<li>4</li>
</ul>
Mixin - MDN Web Docs Glossary: Definitions of Web-related terms | MDN https://developer.mozilla.org/en-US/docs/Glossary/Mixin
Mixin
A mixin is a class or interface in which some or all of its methods and/or propertiesare unimplemented, requiring that another class or interface provide the missing implementations. The new class or interface then includes both the properties and methods from the mixin as well as those it defines itself. All of the methods and properties are used exactly the same regardless of whether they're implemented in the mixin or the interface or class that implements the mixin.
The advantage to mixins is that they can be used to simplify the design of APIs in which multiple interfaces need to include the same methods and properties.
For example, the WindowOrWorkerGlobalScope mixin is used to provide methods and properties that need to be available on both the Window and WorkerGlobalScope interfaces. The mixin is implemented by both of those interfaces.
剩余参数(rest arguments) Mixin的更多相关文章
- 使用剩余参数代替 arguments (prefer-rest-params)
使用剩余参数代替 arguments (prefer-rest-params) 剩余参数来自于ES2016.可以在可变函数中使用这个特性来替代arguments变量. arguments没有Array ...
- ...args剩余参数用法
剩余参数语法允许我们将一个不定数量的参数表示为一个数组. function sum(...theArgs) { return theArgs.reduce((previous, current) ...
- ES6函数剩余参数(Rest Parameters)
我们知道JS函数内部有个arguments对象,可以拿到全部实参.现在ES6给我们带来了一个新的对象,可以拿到除开始参数外的参数,即剩余参数(废话好多 O(∩_∩)O~). 这个新的对象和argume ...
- ES6躬行记(2)——扩展运算符和剩余参数
扩展运算符(Spread Operator)和剩余参数(Rest Parameter)的写法相同,都是在变量或字面量之前加三个点(...),并且只能用于包含Symbol.iterator属性的可迭代对 ...
- ES6学习--函数剩余参数 (rest参数)
ES6 引入 rest 参数(形式为“...变量名”),用于获取函数的多余参数,这样就不需要使用arguments对象了.rest 参数搭配的变量是一个数组,该变量将多余的参数放入数组中.(可以拿到除 ...
- es6笔记 day2---函数默认参数、箭头函数、剩余参数
函数变化: 1.函数默认参数 2.函数参数默认是已经定义了,不能再使用let.const声明 3.扩展运算符.rest运算符 ...就是扩展运算符,它的作用就是把数组给展开 结合函数使用传参,也可以将 ...
- JavaScript 默认参数、动态参数、剩余参数
默认参数: <script> function selet(num, max) { console.log(num + max); } selet(1, 5); </script&g ...
- Atitit 记录方法调用参数上下文arguments
Atitit 记录方法调用参数上下文arguments 1.1. java java8 新的对象Parameter LocalVariableTable 本地变量表 MethodParamete ...
- 传递给函数的隐含参数:arguments及递归函数的实现
传递给函数的隐含参数:arguments当进行函数调用时,除了指定的参数外,还创建一个隐含的对象——arguments.arguments是一个类似数组但不是数组的对象,说它类似是因为它具有数组一样的 ...
随机推荐
- C语言 fork
/* *@author cody *@date 2014-08-12 *@description */ /* #include <sys/types.h> #include <uni ...
- Eclipse中屏蔽日志
如何在Eclipse中屏蔽日志 //屏蔽日志 Eclipse Java import org.apache.log4j.Level; import org.apache.log4j.Logger; L ...
- Sublime Text快捷键去除空白行 - 转载请保留原文链接:https://www.noniu.com/qianduan/sublime-text-kongbaihang.html
如果使用notepad++或者Dreamweaver的朋友,应该知道有个快捷键或者功能按钮,可以实现删除文档空白行的功能.虽然空白行不会影响程序运行,但是会占一定的空间,对于有处女座特质的程序员来说, ...
- [svc][jk]监控jvm的一个坑
监控jvm的一个坑 1,遇到的问题 我按照以往文档,在catalina.sh里追加jvm的监控api,如下 紧接着我启动 tomcat. 未报任何错误. 发现 lsof –i:12000, 12000 ...
- PHP系统学习3 正则
正则 ^shop 标示匹配与shop开头的字符串 shop$用来匹配与shop结尾的字符串 ^shop$只匹配shop [a-z]匹配所有小写字母 [A-Z]匹配所有大写字母 [a-zA-Z]匹配所有 ...
- linux下创建用户(转)
转自 http://www.cnblogs.com/ylan2009/articles/2321177.html Note: 1, Linux Shell 按Tab键不能补全 发现使用新增的用户登陆的 ...
- makefile之patsubst函数
格式:$(patsubst pattern,replacement,text) 名称:模式字符串替换函数--patsubst. 功能:查找text中的单词(单词以"空格".&quo ...
- 李洪强和你一起学习前端之(7)定位盒子 css可见性 滑动门案例
今天是2017年3月23日 1 复习昨天知识 1.1浮动 Float:left | right 特点: ->浮动的元素不占位置(脱标) ->可以将行内元素转化为行内块元素 ->块级元 ...
- 初识md5碰撞与crc32碰撞
现在是晚上23:29.写这篇文章呢,是因为早些时候我胃疼,是因为凉导致的胃疼.凉呢喝了一些热水,喝完热水胃倒是不疼了,但是由于我喝的是茶叶开水,于是就导致失眠了.想来想去这漫漫长夜也没意思,于是就决定 ...
- Unable to parse request org.apache.commons.fileupload.FileUploadBase$IOFileUploadException: Processing of multipart/form-data request failed. null
最近做一个web项目中有上传文件的功能,已经写出并在本地和部署到服务器上测试了好几个文件上传都没问题(我用的是tomcat).部署上服务器,上传图片时有的图片大就回在tomcat日志报如下错误: Un ...