合规国际互联网加速 OSASE为企业客户提供高速稳定SD-WAN国际加速解决方案。 广告
# prototype ## 定义和用法 prototype 属性使您有能力向对象添加属性和方法。 ### 语法 ``` <pre class="calibre13">``` object<span class="token1">.</span>prototype<span class="token1">.</span>name<span class="token">=</span>value ``` ``` ## 实例 在本例中,我们将展示如何使用 prototype 属性来向对象添加属性: ``` <pre class="calibre13">``` <span class="token"><</span>script type<span class="token">=</span><span class="token4">"text/javascript"</span><span class="token">></span> <span class="token2">function</span> <span class="token3">employee</span><span class="token1">(</span>name<span class="token1">,</span>job<span class="token1">,</span>born<span class="token1">)</span> <span class="token1">{</span> <span class="token2">this</span><span class="token1">.</span>name<span class="token">=</span>name<span class="token1">;</span> <span class="token2">this</span><span class="token1">.</span>job<span class="token">=</span>job<span class="token1">;</span> <span class="token2">this</span><span class="token1">.</span>born<span class="token">=</span>born<span class="token1">;</span> <span class="token1">}</span> <span class="token2">var</span> bill<span class="token">=</span><span class="token2">new</span> <span class="token3">employee</span><span class="token1">(</span><span class="token4">"Bill Gates"</span><span class="token1">,</span><span class="token4">"Engineer"</span><span class="token1">,</span><span class="token5">1985</span><span class="token1">)</span><span class="token1">;</span> employee<span class="token1">.</span>prototype<span class="token1">.</span>salary<span class="token">=</span><span class="token2">null</span><span class="token1">;</span> bill<span class="token1">.</span>salary<span class="token">=</span><span class="token5">20000</span><span class="token1">;</span> document<span class="token1">.</span><span class="token3">write</span><span class="token1">(</span>bill<span class="token1">.</span>salary<span class="token1">)</span><span class="token1">;</span> <span class="token"><</span><span class="token">/</span>script<span class="token">></span> ``` ``` 输出: ``` <pre class="calibre14">``` <span class="token5">20000</span> ``` ```