How to define a javascript string.length() function? - Stack Overflow

I am trying to convert java code to javascript (js), and I'm quite frustrated by how js is missing

I am trying to convert java code to javascript (js), and I'm quite frustrated by how js is missing many string methods. I'm aware of js string libraries and that you can do someString.length to get a string's length. But I was pleasantly surprised to see in the top answer to this topic: How to check if a string "StartsWith" another string? That the startsWith method can be defined in my own js code file like so:

if (typeof String.prototype.startsWith != 'function') {
  String.prototype.startsWith = function (str){
    return this.indexOf(str) == 0;
  };
}

So I tried to do something similar for the length method:

if (typeof String.prototype.length != 'function') {
  String.prototype.length = function (){
    return this.length;
  };
}
var str = "a string";
alert(str.length());

But it doesn't work, I get the following error in chrome when I try to call: Uncaught TypeError: Property 'length' of object is not a function

Does anyone know why I can't create a length() function similarly to how it can be done for the startsWith(str) method explained above? Thanks, Keith

I am trying to convert java code to javascript (js), and I'm quite frustrated by how js is missing many string methods. I'm aware of js string libraries and that you can do someString.length to get a string's length. But I was pleasantly surprised to see in the top answer to this topic: How to check if a string "StartsWith" another string? That the startsWith method can be defined in my own js code file like so:

if (typeof String.prototype.startsWith != 'function') {
  String.prototype.startsWith = function (str){
    return this.indexOf(str) == 0;
  };
}

So I tried to do something similar for the length method:

if (typeof String.prototype.length != 'function') {
  String.prototype.length = function (){
    return this.length;
  };
}
var str = "a string";
alert(str.length());

But it doesn't work, I get the following error in chrome when I try to call: Uncaught TypeError: Property 'length' of object is not a function

Does anyone know why I can't create a length() function similarly to how it can be done for the startsWith(str) method explained above? Thanks, Keith

Share Improve this question edited May 23, 2017 at 11:52 CommunityBot 11 silver badge asked Jul 6, 2013 at 17:48 keithphwkeithphw 3901 gold badge4 silver badges14 bronze badges 9
  • 3 Why would you want to turn the existing property into a method? – user395760 Commented Jul 6, 2013 at 17:52
  • 4 Very bad idea. That won't fly for most code. The languages are just too different. Also note that this (if it worked to begin with) would break everyone else's code. – user395760 Commented Jul 6, 2013 at 17:53
  • 4 The first step to translating an application into another language is learning the language to which you wish to translate, not rewriting the language to be like the language you're translating from. – David Thomas Commented Jul 6, 2013 at 18:05
  • 2 You can't cut-and-paste Java into JavaScript any more than you can cut-and-paste Java into C. Please ignore the fact that 'Java' is a substring of 'JavaScript'. That's unfortunate. Try calling it by its official name, ECMAScript instead. – 1983 Commented Jul 6, 2013 at 18:17
  • 3 I find the idiosyncracies of javascript frustrating. In my opinion, it is a poor man's java. I pletely understand your frustration. It's typical of people ing to JS from strongly typed, classical OO languages. If you only consider the Java-like features of JS you are really programming in a poor subset of the language, though. Take a look at this article by Douglas Crawford. You may begin to appreciate JS on its own merits. It is really nothing like Java except for the curly braces. – dodgethesteamroller Commented Jul 6, 2013 at 18:58
 |  Show 4 more ments

2 Answers 2

Reset to default 9

String instances are created with an immutable length property which isn't inherited from String.prototype. So you won't be able to create a length() method for Strings.

See http://ecma-international/ecma-262/5.1/#sec-15.5.5

String instances inherit properties from the String prototype object and their [[Class]] internal property value is "String". String instances also have a [[PrimitiveValue]] internal property, a length property, and a set of enumerable properties with array index names.

And see http://ecma-international/ecma-262/5.1/#sec-15.5.5.1

Once a String object is created, this property is unchanging. It has the attributes { [[Writable]]: false, [[Enumerable]]: false, [[Configurable]]: false }.

It's simple. Just implement the prototype of object.

// Defines the name what You want
String.prototype.size = function () {
   return this.length;
}

'abc'.size ();

If You want some nice implementations, go ahead:

// Defines the name what You want
String.prototype.untilChar = function (charEnding) {
   return charEnding && this.includes (charEnding) ? this.substr (0, this.indexOf (charEnding)).length : this.length;
}

'abcdefg'.untilChar ('d'); // Returns 3
'abcdefg'.untilChar ('z'); // Returns 7

If You can't get the correct solution, don't even try with runarounds! ;)

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744261769a4565667.html

相关推荐

  • How to define a javascript string.length() function? - Stack Overflow

    I am trying to convert java code to javascript (js), and I'm quite frustrated by how js is missing

    8天前
    10

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信