firemail

标题: 这两种定义方法的区别 [打印本页]

作者: hechengjin    时间: 2015-12-9 14:35
标题: 这两种定义方法的区别
  1. ////////////////////////////////
  2. const EXPORTED_SYMBOLS = ['Gloda'];

  3. var Gloda = {
  4.   _init: function gloda_ns_init() {
  5.     this._initLogging();
  6.     GlodaDatastore._init(this._nounIDToDef);
  7.     this._initAttributes();
  8.     this._initMyIdentities();
  9.   },
  10.   
  11.   /**
  12.    * Maps noun IDs to noun definition dictionaries.  (Noun definition
  13.    *  dictionaries provided to us at the time a noun was defined, plus some
  14.    *  additional stuff we put in there.)
  15.    */
  16.   _nounIDToDef: {},
  17. };

  18. try {
  19.   Gloda._init();
  20. }

  21. //////////////////////////////

  22. function PostCommitHandler(aCallbacks) {
  23.   this.callbacks = aCallbacks;
  24.   GlodaDatastore._pendingAsyncStatements++;
  25. }
  26. PostCommitHandler.prototype = {
  27. ..
  28. };

  29. -----------------
  30. 这两种写法的区别?
复制代码

作者: jimu    时间: 2015-12-9 22:20
第二种方法的调用 :

let pcm = new PostCommitHandler(this._pendingPostCommitCallbacks);




对象字面量:

var a={}等同于var a=new Object;

   var b=[]等同于var b=new Array;

   var c={name:"syj",id:"0"};等同于

   var c=new Object;

  c.name="syj";

  c.id="0";

这下大家明白了吧,其实这样写更加简单,而且节省空间,对javascript这种非编译型语言节省空间也很重要啊。

在文章的最后在举一个或许让很多初学者感到疑问的东西

  <script type="text/javascript">   <!--   代码块   //-->   </script>

经常可以看到有人这样写javascript,为什么要加<!-- //-->呢,其实不加也是可以运行的,这是为了兼容不支持javascript浏览器而做的事情,如果浏览器不支持javascript的话加了<!-- //-->页面是不会报错的,现在浏览器都支持了,大可不必这样写了。




作者: jimu    时间: 2015-12-9 22:22
  1. //对象字面量只能添加静态属性和方法
  2.    var myObject={
  3.      propertyA: sha ,
  4.      propertyB: feng ,
  5.      methodA:function(){
  6.       alert(this.propertyA+ +this.propertyB);
  7.      },
  8.      methodB:function(){}
  9.     }
  10.    
  11.     myObject.methodA();
  12.    
  13.    
  14.    //利用prototype属性可以添加公有属性和方法
  15.    
  16.    function myConstructor2(){};  //声明构造函数,可以使用对象字面量语法来向prototype属性中添加所有公有成员
  17.    
  18.     myConstructor2.prototype={
  19.      propertyA: sha ,
  20.      propertyB: feng ,
  21.      methodA:function(){
  22.       alert(this.propertyA+ +this.propertyB);
  23.      },
  24.      methodB:function(){}
  25.     }
  26.    
  27.    var myconstrustor=new myConstructor2(); //声明对象
  28.     myconstrustor.methodA();
复制代码





欢迎光临 firemail (http://www.firemail.wang:8088/) Powered by Discuz! X3