javascript - Why does 'this' inside String.prototype refer to an object type, not a string type? - Stack Overflo

I'm trying to extend string to provide a hash of itself. I am using the Node.js crypto library.I e

I'm trying to extend string to provide a hash of itself. I am using the Node.js crypto library.

I extend string like this:

String.prototype.hashCode = function() {
    return getHash(this);
};

and I have a getHash function that looks like this:

function getHash(testString) {
    console.log("type is" + typeof(testString));
    var crypto = require('crypto');
    var hash = crypto.createHash("sha256");
    hash.update(testString);
    var result = hash.digest('hex');
    return result;
}

The function works fine when called directly, as in

var s = "Hello world";
console.log(getHash(s));

but when I try:

var s = "ABCDE";
console.log(s.hashCode());

the method call fails. It appears that this in the String.prototype.hashCode is identified as an object when crypto.hash.update is called, but a string is expected. I thought that this inside String.prototype would be the string itself, but for some reason it looks like an object to getHash(). How can I fix it?

I'm trying to extend string to provide a hash of itself. I am using the Node.js crypto library.

I extend string like this:

String.prototype.hashCode = function() {
    return getHash(this);
};

and I have a getHash function that looks like this:

function getHash(testString) {
    console.log("type is" + typeof(testString));
    var crypto = require('crypto');
    var hash = crypto.createHash("sha256");
    hash.update(testString);
    var result = hash.digest('hex');
    return result;
}

The function works fine when called directly, as in

var s = "Hello world";
console.log(getHash(s));

but when I try:

var s = "ABCDE";
console.log(s.hashCode());

the method call fails. It appears that this in the String.prototype.hashCode is identified as an object when crypto.hash.update is called, but a string is expected. I thought that this inside String.prototype would be the string itself, but for some reason it looks like an object to getHash(). How can I fix it?

Share Improve this question edited Jan 2, 2015 at 3:14 Ry- 225k56 gold badges493 silver badges499 bronze badges asked Jan 2, 2015 at 3:04 GGizmosGGizmos 3,7834 gold badges33 silver badges88 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 8

this can’t be of a primitive type outside of strict mode, so it bees a String wrapper type, which doesn’t behave like a primitive string at all (particularly as far as typeof and equality – both strict and loose – go). You can cast it:

String.prototype.hashCode = function () {
    return getHash('' + this);
};

where '' + is used to cast any value to a primitive string. (String(this) also works, if you feel that it’s clearer.)

You can also go into strict mode, where things just make sense:

String.prototype.hashCode = function () {
    'use strict';
    return getHash(this);
};

When you call a method on a variable of primitive type, so-called auto-boxing is taken place. That process wraps a primitive value into corresponding object, for example 'asdf' to new String('asdf'). Because technically primitive values don't have methods and properties, they are hosted in object prototypes. With auto-boxing you could call methods on primitive values. And within a method, this is always the object that has that method.

If you want to access the primitive value in a method, you could either pass it as an argument, or as you would like, retrieve primitive value from this. For example:

var str = new String('asdf') // String {0: "a", 1: "s", 2: "d", 3: "f", length: 4, formatUnicorn: function, truncate: function, splitOnLast: function, [[PrimitiveValue]]: "asdf"}
String(str) // 'asdf'

var num = new Number(123) // Number {[[PrimitiveValue]]: 123}
Number(num) // 123

var bool = new Boolean(true) // Boolean {[[PrimitiveValue]]: true}
Boolean(bool) // true

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

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

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

关注微信