Ruby's Louvre

每天学习一点点算法

导航

< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

统计

sass的类型判定

由于sass的作者是rubyer,因此它的类型与JS有点不一样,但一样可以类推。

@charset "utf-8";//必须设置了这个才能编译有中文的注释
 
 
$gray: #333;//颜色
$number: 12;//数字
$boolean: true;
$array:();
$string:"string";
$string2: 'string';
$null:null;
$map: (
    aa:1,
    bb:2
);
@mixin box-shadow($shadows) {
  -moz-box-shadow: $shadows;
  -webkit-box-shadow: $shadows;
  box-shadow: $shadows;
}
 
.aaa {
    content: type-of($gray);
}
.bbb {
    content: type-of($number);
}
.ccc{
    content: type-of($boolean);
}
.ddd {
    content: type-of($array);
}
.eee {
    content: type-of($string);
}
.fff {
    content: type-of($string2);
}
.ggg {
    content: type-of($null);
}
.hhh {
    content: type-of(type-of);
}
.iii {
    content: type-of($map);
}
.kkk {
    content: type-of(box-shadow);
}
//--------------------------------------
.aaa {
  content: color; }
 
.bbb {
  content: number; }
 
.ccc {
  content: bool; }
 
.ddd {
  content: list; }
 
.eee {
  content: string; }
 
.fff {
  content: string; }
 
.ggg {
  content: null; }
 
.hhh {
  content: string; }
 
.iii {
  content: map; }
 
.kkk {
  content: string; }

可以看到sass的类型有null, string, bool, number, list, map, color这几种类型,如果直接拿内置函数的名字或@mixin函数放进type-of里,都是得到字符串。

但type-of不能判定@function函数与@mixin函数,这时就需要用到function_exists, mixin_exists这两个方法了,但我觉得它们的名字起得不好,不是中杠线连在一起!

@charset "utf-8";//必须设置了这个才能编译有中文的注释
 
@mixin box-shadow($shadows) {
  -moz-box-shadow: $shadows;
  -webkit-box-shadow: $shadows;
  box-shadow: $shadows;
}
 
 
 
.testvar1{
     content: function_exists("type-of");
}
.testvar2{
     content: mixin_exists("box-shadow");
}
//--------------------------------------
.testvar1 {
  content: true; }
 
.testvar2 {
  content: true; }

有了类型判定,我们就可以玩参数泛型化了。

如果您觉得此文有帮助,可以打赏点钱给我支付宝1669866773@qq.com ,或扫描二维码

posted on   司徒正美  阅读(2566)  评论(0编辑  收藏  举报

编辑推荐:
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
阅读排行:
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· 分享 3 个 .NET 开源的文件压缩处理库,助力快速实现文件压缩解压功能!
· Ollama——大语言模型本地部署的极速利器
· DeepSeek如何颠覆传统软件测试?测试工程师会被淘汰吗?
历史上的今天:
2013-06-27 最火的前端开源项目
2011-06-27 python学习笔记—— 函数
点击右上角即可分享
微信分享提示