mirror of
https://github.com/fofolee/uTools-Manuals.git
synced 2025-12-15 07:06:53 +08:00
语法高亮,滚动条美化,设置页面调整
This commit is contained in:
@@ -5,7 +5,7 @@
|
||||
</div>
|
||||
<p><code><strong>watch() </strong></code>方法会监视属性是否被赋值并在赋值时运行相关函数。</p>
|
||||
<h2 id="Summary" name="Summary">语法</h2>
|
||||
<pre class="syntaxbox"><em>obj</em>.watch(<em>prop</em>, <em>handler</em>)</pre>
|
||||
<pre><code class="language-javascript"><em>obj</em>.watch(<em>prop</em>, <em>handler</em>)</code></pre>
|
||||
<h3 id="Parameters" name="Parameters">参数</h3>
|
||||
<dl>
|
||||
<dt><code>prop</code></dt>
|
||||
@@ -26,7 +26,7 @@
|
||||
<div class="note"><strong>注意:</strong> Calling <code>watch()</code> on an object for a specific property overrides and previous handler attached for that property.</div>
|
||||
<h2 id="Examples" name="Examples">例子</h2>
|
||||
<h3 id="Example:_Using_watch_and_unwatch" name="Example:_Using_watch_and_unwatch">使用 <code>watch</code> 和 <code>unwatch</code></h3>
|
||||
<pre class="brush: js">var o = {p:1};
|
||||
<pre><code class="language-javascript">var o = {p:1};
|
||||
o.watch("p",
|
||||
function (id, oldval, newval) {
|
||||
console.log("o." + id + "由" + oldval + " 变为 " + newval);
|
||||
@@ -40,15 +40,15 @@ o.p = 4;
|
||||
|
||||
o.unwatch('p');
|
||||
o.p = 5;
|
||||
</pre>
|
||||
</code></pre>
|
||||
<p>上面的代码显示结果如下:</p>
|
||||
<pre class="eval">o.p 由 1 变为 2
|
||||
o.p 由 2 变为 3
|
||||
o.p 由 undefined 变为 4
|
||||
</pre>
|
||||
</code></pre>
|
||||
<h3 id="Example:_Using_watch_to_validate_an_object.27s_properties" name="Example:_Using_watch_to_validate_an_object.27s_properties">使用 <code>watch</code> 来验证一个对象的属性</h3>
|
||||
<p>你可以使用 <code>watch</code> 来检测一个对象的属性赋值是否是合法的.下例演示了如何确保每个人始终具有一个合法的名字和0 到 200之间的年龄.</p>
|
||||
<pre class="brush: js">Person = function(name,age) {
|
||||
<pre><code class="language-javascript">Person = function(name,age) {
|
||||
this.watch("age", Person.prototype._isValidAssignment);
|
||||
this.watch("name", Person.prototype._isValidAssignment);
|
||||
this.name = name;
|
||||
@@ -84,12 +84,12 @@ try {
|
||||
} catch (e) {
|
||||
console.log(e);
|
||||
}
|
||||
</pre>
|
||||
</code></pre>
|
||||
<p>上面的代码显示结果如下:</p>
|
||||
<pre class="eval">Will, 29
|
||||
RangeError: 不合法的名字 Will, 29
|
||||
RangeError: 不合法的年龄 Will, 29
|
||||
</pre>
|
||||
</code></pre>
|
||||
<h2 id="Specifications">Specifications</h2>
|
||||
<p>Not part of any specifications. Implemented in JavaScript 1.2.</p>
|
||||
<h2 id="Browser_compatibility">Browser compatibility</h2>
|
||||
|
||||
Reference in New Issue
Block a user