Discuz! Board

 找回密码
 立即注册
搜索
热搜: 活动 交友 discuz
查看: 1909|回复: 2
打印 上一主题 下一主题

这两种定义方法的区别

[复制链接]

388

主题

602

帖子

2218

积分

金牌会员

Rank: 6Rank: 6

积分
2218
跳转到指定楼层
楼主
发表于 2015-12-9 14:35:45 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
  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. 这两种写法的区别?
复制代码
回复

使用道具 举报

257

主题

354

帖子

1677

积分

金牌会员

Rank: 6Rank: 6

积分
1677
沙发
发表于 2015-12-9 22:20:14 | 只看该作者
第二种方法的调用 :

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的话加了<!-- //-->页面是不会报错的,现在浏览器都支持了,大可不必这样写了。



回复 支持 反对

使用道具 举报

257

主题

354

帖子

1677

积分

金牌会员

Rank: 6Rank: 6

积分
1677
板凳
发表于 2015-12-9 22:22:38 | 只看该作者
  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();
复制代码
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|Archiver|手机版|小黑屋|firemail ( 粤ICP备15085507号-1 )

GMT+8, 2024-5-8 22:21 , Processed in 0.057003 second(s), 18 queries .

Powered by Discuz! X3

© 2001-2013 Comsenz Inc.

快速回复 返回顶部 返回列表