mirror of
https://github.com/thinkjs/thinkjs.git
synced 2026-01-25 14:42:47 +00:00
419 lines
10 KiB
HTML
419 lines
10 KiB
HTML
<% include inc/header.html %>
|
||
<div class="container" style="margin-top:60px;">
|
||
<div class="row">
|
||
<h2>原生对象的扩展</h2>
|
||
<h4>Object.values(obj)</h4>
|
||
<ul>
|
||
<li>obj <code>Object</code></li>
|
||
<li>return: <code>Array</code></li>
|
||
</ul>
|
||
<p>获取对象值的集合</p>
|
||
|
||
<h4>Array.prototype.sum</h4>
|
||
<ul>
|
||
<li>return: <code>Number</code></li>
|
||
</ul>
|
||
<p>获取数组的和</p>
|
||
|
||
<h2>全局函数</h2>
|
||
<h4>Class(obj)</h4>
|
||
<ul>
|
||
<li>obj <code>function | object</code> 如果是function, 则自动执行这个function,获取返回结果</li>
|
||
<li>return <code>Class</code></li>
|
||
</ul>
|
||
<p>通过该函数可以创建一个Class,实例化的时候可以省略new, 并且自动调用init方法。</p>
|
||
<pre><code class="language-js">var A = Class(function(){
|
||
return {
|
||
init: function(name){
|
||
this.name = name;
|
||
},
|
||
mth: function(){
|
||
|
||
}
|
||
}
|
||
}).extend({
|
||
othermth: function(){
|
||
console.log("mth")
|
||
}
|
||
})
|
||
var a = A("welefen");
|
||
console.log(a.name); //will be `welefen`
|
||
a.mth();</code></pre>
|
||
|
||
<h4>extend(target, source1, source2, ...)</h4>
|
||
<ul>
|
||
<li>target <code>object</code> </li>
|
||
<li>source <code>object</code> </li>
|
||
</ul>
|
||
<p>复制对象的方法对另一个对象上,默认为深度复制。如果第一个参数为false, 则为浅度复制。该方法与jQuery的extend相似,只是默认为深度复制。</p>
|
||
|
||
<h4>isBoolean(obj)</h4>
|
||
<ul>
|
||
<li>obj <code>mixed</code> </li>
|
||
<li>return <code>true | false</code></li>
|
||
</ul>
|
||
<p>是否为布尔值</p>
|
||
|
||
<h4>isNumber(obj)</h4>
|
||
<ul>
|
||
<li>obj <code>mixed</code> </li>
|
||
<li>return <code>true | false</code></li>
|
||
</ul>
|
||
<p>是否为数值</p>
|
||
|
||
<h4>isObject(object)</h4>
|
||
<ul>
|
||
<li>obj <code>mixed</code> </li>
|
||
<li>return <code>true | false</code></li>
|
||
</ul>
|
||
<p>是否为object对象</p>
|
||
|
||
|
||
<h4>isString(obj)</h4>
|
||
<ul>
|
||
<li>obj <code>mixed</code> </li>
|
||
<li>return <code>true | false</code></li>
|
||
</ul>
|
||
<p>是否为字符串</p>
|
||
|
||
<h4>isFunction(obj)</h4>
|
||
<ul>
|
||
<li>obj <code>mixed</code> </li>
|
||
<li>return <code>true | false</code></li>
|
||
</ul>
|
||
<p>是否为函数</p>
|
||
|
||
|
||
<h4>isDate(obj)</h4>
|
||
<ul>
|
||
<li>obj <code>mixed</code> </li>
|
||
<li>return <code>true | false</code></li>
|
||
</ul>
|
||
<p>是否为日期对象</p>
|
||
|
||
<h4>isRegexp(obj)</h4>
|
||
<ul>
|
||
<li>obj <code>mixed</code> </li>
|
||
<li>return <code>true | false</code></li>
|
||
</ul>
|
||
<p>是否为正则表达式对象</p>
|
||
|
||
|
||
<h4>isError(obj)</h4>
|
||
<ul>
|
||
<li>obj <code>mixed</code> </li>
|
||
<li>return <code>true | false</code></li>
|
||
</ul>
|
||
<p>是否为错误对象</p>
|
||
|
||
|
||
<h4>isEmpty(obj)</h4>
|
||
<ul>
|
||
<li>obj <code>mixed</code> </li>
|
||
<li>return <code>true | false</code></li>
|
||
</ul>
|
||
<p>是否为空,false | "" | [] | {} | 0 | null | undefined 为true, 其他值为false。</p>
|
||
|
||
<h4>isArray(obj)</h4>
|
||
<ul>
|
||
<li>obj <code>mixed</code> </li>
|
||
<li>return <code>true | false</code></li>
|
||
</ul>
|
||
<p>是否为数组</p>
|
||
|
||
|
||
<h4>isIP(obj)</h4>
|
||
<ul>
|
||
<li>obj <code>mixed</code> </li>
|
||
<li>return <code>true | false</code></li>
|
||
</ul>
|
||
<p>是否为IP值,IP4 | IP6。</p>
|
||
|
||
<h4>isIP4(obj)</h4>
|
||
<ul>
|
||
<li>obj <code>mixed</code> </li>
|
||
<li>return <code>true | false</code></li>
|
||
</ul>
|
||
<p>是否为IP4。</p>
|
||
|
||
<h4>isIP6</h4>
|
||
<ul>
|
||
<li>obj <code>mixed</code> </li>
|
||
<li>return <code>true | false</code></li>
|
||
</ul>
|
||
<p>是否为IP6。</p>
|
||
|
||
|
||
<h4>isFile(file)</h4>
|
||
<ul>
|
||
<li>obj <code>string</code> </li>
|
||
<li>return <code>true | false</code></li>
|
||
</ul>
|
||
<p>是否是个文件,如果不存在,则为false。</p>
|
||
|
||
|
||
<h4>isDir</h4>
|
||
<ul>
|
||
<li>obj <code>string</code> </li>
|
||
<li>return <code>true | false</code></li>
|
||
</ul>
|
||
<p>是否是个目录,如果不存在,则为false。</p>
|
||
|
||
|
||
<h4>isBuffer(obj)</h4>
|
||
<ul>
|
||
<li>obj <code>mixed</code> </li>
|
||
<li>return <code>true | false</code></li>
|
||
</ul>
|
||
<p>是否Buffer对象。</p>
|
||
|
||
|
||
<h4>isPromise(obj)</h4>
|
||
<ul>
|
||
<li>obj <code>mixed</code> </li>
|
||
<li>return <code>true | false</code></li>
|
||
</ul>
|
||
<p>是否是Promise。</p>
|
||
|
||
|
||
<h4>isWritable(path)</h4>
|
||
<ul>
|
||
<li>path <code>string</code> </li>
|
||
<li>return <code>true | false</code></li>
|
||
</ul>
|
||
<p>目录是否可写。</p>
|
||
|
||
<h4>mkdir(p, mode)</h4>
|
||
<ul>
|
||
<li>p <code>string</code> 要创建的目录</li>
|
||
<li>mode <code>string</code> 目录权限,默认为0755</li>
|
||
</ul>
|
||
<p>同步模式递归创建一个目录。</p>
|
||
|
||
<h4>chmod(p, mode)</h4>
|
||
<ul>
|
||
<li>p <code>string</code> 要修改的目录</li>
|
||
<li>mode <code>string</code> 目录权限,默认为0755</li>
|
||
</ul>
|
||
<p>同步模式修改目录的权限。</p>
|
||
|
||
|
||
<!--<h4>getFileContent(filepath)</h4>
|
||
<ul>
|
||
<li>filepath <code>string</code> 文件的路径</li>
|
||
<li>return <code>string</code> 获取到的文件内容,如果文件不存在,则为""</li>
|
||
</ul>
|
||
<p>同步模式获取文件的内容,文件编码为C('encoding')。</p>
|
||
|
||
|
||
<h4>setFileContent(filepath, content)</h4>
|
||
<ul>
|
||
<li>filepath <code>string</code> 文件的路径</li>
|
||
<li>content <code>string</code> 要写入的内容</li>
|
||
</ul>
|
||
<p>同步模式写入文件内容。</p> -->
|
||
|
||
|
||
<h4>md5(string)</h4>
|
||
<ul>
|
||
<li>string <code>string</code> 要计算的字符串</li>
|
||
<li>return <code>string</code> 内容的md5值</li>
|
||
</ul>
|
||
<p>获取字符串的md5值。</p>
|
||
|
||
|
||
<h4>getPromise(obj, reject)</h4>
|
||
<ul>
|
||
<li>obj <code>mix</code> 传入的对象会直接返回</li>
|
||
<li>reject <code>boolean</code> 是否为reject模式,默认为resolve模式</li>
|
||
<li>return <code>Promise</code> 返回一个Promise对象</li>
|
||
</ul>
|
||
|
||
|
||
<h4>getDefer</h4>
|
||
<ul>
|
||
<li>return <code>Deferred</code> 返回一个Deferred对象</li>
|
||
</ul>
|
||
<p>获取一个Deferred对象,含有resolve和reject方法,以及promise属性。</p>
|
||
|
||
|
||
<h4>getObject(name, value)</h4>
|
||
<ul>
|
||
<li>name <code>string|array</code> 要生成的对象的key,可以是个字符串或者多个字符串对应的数组</li>
|
||
<li>value <code>mix</code> 要生成的对象的value</li>
|
||
<li>return <code>Object</code></li>
|
||
</ul>
|
||
<p>快速生成一个Object,主要是方便name和value都是动态的时候生成。</p>
|
||
|
||
|
||
<h4>thinkRequire(name)</h4>
|
||
<ul>
|
||
<li>name <code>string</code> 要加载的模块名</li>
|
||
<li>return <code>mix</code> 返回模块对象</li>
|
||
</ul>
|
||
<p>thinkRequire相对于原生的require,多了根据特定的后缀自动查找的功能。类似于php里的autoload机制。</p>
|
||
<p>如:<code>thinkRequire("UserModel")</code>会自动从几个存放Model的目录查找UserModel模块,不需要手工写很长的文件路径。</p>
|
||
|
||
<h4>inherits(superClass, methods)</h4>
|
||
<ul>
|
||
<li>superClass <code>string</code> 父类,会自动调用thinkRequire去加载</li>
|
||
<li>methods <code>object</code> 要追加的属性和方法集合</li>
|
||
<li>return <code>Class</code> 通过Class函数生成的类</li>
|
||
</ul>
|
||
<!--
|
||
|
||
<h4>Cache(superClass, methods)</h4>
|
||
<ul>
|
||
<li>superClass <code>string</code> </li>
|
||
<li>methods <code>object </code> 要追加的属性和方法集合</li>
|
||
<li>return <code>Class</code> 返回一个Cache子类</li>
|
||
</ul>
|
||
<pre><code>var xxCache = Cache(function(){
|
||
return {
|
||
xxx: function(){
|
||
|
||
}
|
||
}
|
||
})</code></pre>
|
||
|
||
<h4>Behavior</h4>
|
||
<h4>Controller</h4>
|
||
<h4>Session</h4>
|
||
<h4>Model</h4>
|
||
<h4>Db</h4>
|
||
<h4>B</h4>
|
||
<h4>tag</h4> -->
|
||
<h4>C(name, value)</h4>
|
||
<ul>
|
||
<li>name <code>string</code> 配置名称</li>
|
||
<li>value <code>mix</code> 配置值</li>
|
||
</ul>
|
||
<ul>
|
||
<li>C('name', 'welefen') 设置配置值</li>
|
||
<li>C('name') 获取名为name的配置值</li>
|
||
<li>C({name: "name", value: "value"}) 设置配置值</li>
|
||
</ul>
|
||
<h4>A</h4>
|
||
<h4>F</h4>
|
||
<h4>D</h4>
|
||
<h4>S</h4>
|
||
<h4>E</h4>
|
||
|
||
<h2>Controller类的方法</h2>
|
||
|
||
<h4>controller.ip()</h4>
|
||
<ul>
|
||
<li>return: <code>String</code></li>
|
||
</ul>
|
||
<p>获取当前用户的ip</p>
|
||
|
||
<h4>controller.isGet()</h4>
|
||
<ul>
|
||
<li>return: <code>Boolean</code></li>
|
||
</ul>
|
||
<p>是否为GET请求</p>
|
||
|
||
<h4>controller.isPost()</h4>
|
||
<ul>
|
||
<li>return: <code>Boolean</code></li>
|
||
</ul>
|
||
<p>是否为POST请求</p>
|
||
|
||
<h4>controller.isMethod(method)</h4>
|
||
<ul>
|
||
<li>method <code>String</code> 请求类型</li>
|
||
<li>return: <code>Boolean</code></li>
|
||
</ul>
|
||
<p>判断当前请求类型与传递的参数是否相同</p>
|
||
|
||
<h4>controller.isAjax()</h4>
|
||
<ul>
|
||
<li>return: <code>Boolean</code></li>
|
||
</ul>
|
||
<p>是否为ajax请求</p>
|
||
|
||
<h4>controller.get(name)</h4>
|
||
<ul>
|
||
<li>name <code>String|undefined</code></li>
|
||
<li>return: <code>String|Object</code></li>
|
||
</ul>
|
||
<p>获取get参数的值,如果name为undefined,则返回所有的参数值</p>
|
||
|
||
<h4>controller.post(name)</h4>
|
||
<ul>
|
||
<li>name <code>String|undefined</code></li>
|
||
<li>return: <code>String|Object</code></li>
|
||
</ul>
|
||
<p>获取POST参数的值,如果name为undefined,则返回所有的POST值</p>
|
||
|
||
<h4>controller.param(name)</h4>
|
||
<ul>
|
||
<li>name <code>String|undefined</code></li>
|
||
<li>return: <code>String|Object</code></li>
|
||
</ul>
|
||
<p>获取POST或者GET参数的值(优先从POST里获取),如果name为undefined,则返回所有的值</p>
|
||
|
||
<h4>controller.file(name)</h4>
|
||
<ul>
|
||
<li>name <code>String</code></li>
|
||
<li>return: <code>Object</code></li>
|
||
</ul>
|
||
<p>获取上传的文件对象</p>
|
||
|
||
<h4>controller.header(name, value)</h4>
|
||
<p>获取或者设置header</p>
|
||
<ul>
|
||
<li>如果name为object, 则循环设置header</li>
|
||
<li>如果设置了value,则设置header</li>
|
||
<li>如果name为undefined,则返回所有的header</li>
|
||
<li>返回name对应的header值</li>
|
||
</ul>
|
||
|
||
<h4>controller.userAgent()</h4>
|
||
<ul>
|
||
<li>return: <code>String</code></li>
|
||
</ul>
|
||
<p>返回用户的userAgent</p>
|
||
|
||
<h4>controller.referrer()</h4>
|
||
<ul>
|
||
<li>return: <code>String</code></li>
|
||
</ul>
|
||
<p>返回当前请求的referrer</p>
|
||
|
||
<h4>controller.cookie(name, value, options)</h4>
|
||
|
||
<h4>controller.session(name, value)</h4>
|
||
|
||
<h4>controller.redirect(url, code)</h4>
|
||
|
||
<h4>controller.assign(name, value)</h4>
|
||
|
||
<h4>controller.fetch(templateFile, content)</h4>
|
||
|
||
<h4>controller.display(templateFile, charset, contentType, content)</h4>
|
||
|
||
<h4>controller.action(action)</h4>
|
||
|
||
<h4>controller.jsonp(data)</h4>
|
||
|
||
<h4>controller.json(data)</h4>
|
||
|
||
<h4>controller.status(status)</h4>
|
||
|
||
<h4>controller.echo(obj, encoding)</h4>
|
||
|
||
<h4>controller.end(obj)</h4>
|
||
|
||
<h4>controller.download(file, contentType)</h4>
|
||
|
||
<h4>controller.success(data)</h4>
|
||
|
||
<h4>controller.error(data)</h4>
|
||
|
||
<h2>Model类的方法</h2>
|
||
</div>
|
||
</div>
|
||
|
||
<script src="/static/module/highlight/highlight.pack.js"></script>
|
||
<script >hljs.initHighlightingOnLoad();</script> |