Category

Archive for the 'Javascript' Category

获得Javascript 对象的属性个数

  1. //扩展对象的count方法
  2. Object.prototype.count = (
  3.     Object.prototype.hasOwnProperty(‘__count__’)
  4.   ) ? function () {
  5.     return this.__count__;
  6.   } : function () {
  7.     var count = 0;
  8.     for (var i in this) if (this.hasOwnProperty(i)) {
  9.       count ++;
  10.     }
  11.     return count;
  12.   };
  13.  
  14. //使用
  15. var myObj = {
  16.     name1: “value1″,
  17.     name2: “value2″
  18. };
  19.  
  20. alert(myObj.count());

<script language=”javascript” type=”text/javascript” charset=”utf-8″>

Just a reminder, the <script> tag, in all browsers that support it, defaults to being interpreted as the javascript language, of mime-type text/javascript, and of charset UTF-8. You don’t need to specify any of these values.

In fact, you can’t reliably change the charset, even with the charset attribute, so it’s generally best not to try. Just always use UTF-8 for javascript, 100% of the time, and you’re fine.

If you want to use the <script> tag for some clever client-side purpose (for example, client-side templates in OpenSocial), then you need to change the “language” attribute to something that the browser won’t understand, so that it will not try to execute the script as javascript.

If you want to use e4x in javascript in Firefox, you need to specify this on the type attribute like so: type=”text/javascript; e4x=1″. In order to use Javascript 1.8 in Firefox 3.1+, you can do this: type=”application/javascript;version=1.8″.

If you are building pages for MSIE, and you want to use VBScript, you will need to specify this in the language attribute: <script language=”vbscript”>

If you’re building pages for Titanium, you can use Python or Ruby instead of javascript like this: <script type=”text/python”>, <script type=”text/ruby”>.

So, the type and language attributes on the script tag have some value if you’re using proprietary extensions in specific browsers, or if you want browsers to ignore the <script> block altogether.

If you’re writing javascript for A-grade browsers (which is, I believe, the most common use case among FEs at Yahoo), you can just use an unadorned <script> tag.

Douglas Crockford 在google做的techtalks

Douglas Crockford 在google做的techtalks 主题是Gears and the Mashup Problem(Gears 和 Mashup的问题)
老头还是很幽默,首先出示了他的加州驾照和雅虎工卡以“验明正身”。