玉器内部重用function

我有一个.jade文件中重复2次内联脚本和代码块,并希望:

  • 重新使用它。 (我的意思是干它,只有一个块/function)
  • 这里build议的那样转义html,现在我正在使用!= linkExist('foo')

我的想法是使用mixin ,但不知道如何。 我的代码原样,但想知道如何写得更好。 考虑到codereview (因为我的代码实际上工作,我只是想改善它),但玉甚至没有一个标签呢,所以我觉得SO可能会更好。

 h1 Teachers for result in object.teachers - var linkExist = function(i){ - if (result[i] != 'undefined'){ - var html = ', follow on ' + i + ': <a href="' + result[i] + '" target="_blank">' + result[i].split("http://")[1] + '</a>'; - return html; - }; - } section h3 #{result.Name} p.inline #{result.Nick} img(src=result.img) p.small Location: #{result.Location} p.small | Web: for webResult in result.Web a(href=webResult,target='_blank') #{webResult.split('http://')[1]} != linkExist('Twitter') != linkExist('GitHub') //now it repeats the code but for students h1 Students for result in object.students - var linkExist = function(i){ //etc....... 

你应该可以使用混合; 如果你传递result ,它应该是非常通用的:

 mixin linkExist(result, type) if result[type] !== undefined | , follow on #{type}: <a href="#{result[type]}">...</a> //- use like this for result in object.teachers ... mixin linkExist(result, 'Twitter') mixin linkExist(result, 'GitHub') for result in object.students ... mixin linkExist(result, 'Twitter') mixin linkExist(result, 'GitHub')