在ES6中扩展String时出错

时间:2016-01-15 12:26:25

标签: javascript class ecmascript-6



 $date = getdate();
 $guid = NEWID();

$data = array(
   'item_id'            => $item_id,
   'detail_image_name'  => $name,
   'created_on'         => $date,
   'created_by'         => $this->session->userdata('username'),
   'guid'               => $guid
);

$this->db->insert('product_detail_image', $data); 




当我运行此代码时,我遇到了错误:

 'use strict';

 class ReverseString extends String {
   reversed() {
     let res = '';
     for (let i = this.length - 1; i >= 0; --i) {
       res += this[i];
     }
     return res;
   }
 }

 let rs = new ReverseString("wangyang");
 console.log(rs.reversed());

我无法找到此错误的根本原因。

C:\Users\elqstux\Desktop>node wy.js C:\Users\elqstux\Desktop\wy.js:14 console.log(rs.reversed()); ^ TypeError: rs.reversed is not a function at Object.<anonymous> (C:\Users\elqstux\Desktop\wy.js:14:16) at Module._compile (module.js:398:26) at Object.Module._extensions..js (module.js:405:10) at Module.load (module.js:344:32) at Function.Module._load (module.js:301:12) at Function.Module.runMain (module.js:430:10) at startup (node.js:141:18) at node.js:980:3 的输出为console.log(rs);

这是我的节点版本:

String {0: "w", 1: "a", 2: "n", 3: "g", 4: "y", 5: "a", 6: "n", 7: "g", length: 8, [[PrimitiveValue]]: "wangyang"}]

1 个答案:

答案 0 :(得分:2)

根据:

String目前在Node 5.3中不是子类

https://kangax.github.io/compat-table/es6/#test-miscellaneous_subclassables

您的示例应该可以在Firefox 45+和Edge 13 +上正常工作