type-coercion

JavaScript type coercion with strings and indexing

Deadly 提交于 2021-02-07 10:14:31
问题 In the below snippet why does whatDoesItDo() function return "fail" as string? It would be helpful if someone can explain the concept behind such behavior. function whatDoesItDo() { return (![] + [])[+[]] + (![] + [])[+!+[]] + ([![]] + [][ [] ])[+!+[] + [+[]]] + (![] + [])[!+[] + !+[]]; } function result() { document.getElementById("result").innerHTML = whatDoesItDo(); } result(); <html> <body> <p id="result"></p> </body> </html> 回答1: You're seeing the effects of A) Type coercion, B) Indexing

Literal Int to Double coercion in arithmetic expressions [duplicate]

筅森魡賤 提交于 2020-02-06 09:03:25
问题 This question already has an answer here : Strange Swift numbers type casting (1 answer) Closed 3 months ago . It appears that Swift applies floating point contagion (as it is called in other languages) to literal Int operands in an expression containing a Double variable before evaluating the expression. Is there an explicit statement about that somewhere? I wasn't able to find a specific description about what to expect. For example, suppose I have let b = 0.14 . Then the following all

Why, in JavaScript, does '3 instanceof Number' == false, but '3..method()' will call Number.prototype.method?

风格不统一 提交于 2020-01-10 04:47:05
问题 Given that a literal number not strictly an instance of Number, why can I call prototype methods of Number (or String, or Boolean) objects on the corresponding literal objects? Is this standard behavior across browsers? What exactly is happening when this occurs? I suspect it's coercing the literal into the corresponding type before calling the method, because when I inspect typeof this in the method, it's returning 'object' rather than 'number'. 回答1: The literal is not coerced into an

Understanding JavaScript hoisting and truthy & falsy

别说谁变了你拦得住时间么 提交于 2019-12-29 00:35:55
问题 I've been reading about JavaScript hoisting sometime back. JavaScript Scoping and Hoisting by Ben Cherry Two words about “hoisting” by Dmitry Soshnikov and, some more about JavaScript type-coercion, truth & false test: Truth, Equality and JavaScript and some other resource And while practicing some, and found I m missing some important concept about the hoisting and a variable' truthy & falsy. 1: 'if' truth test with duplicate variable declaration var foo = 1; function bar() { if (!foo) {

In Ruby, how does coerce() actually work?

∥☆過路亽.° 提交于 2019-12-28 02:27:06
问题 It is said that when we have a class Point and knows how to perform point * 3 like the following: class Point def initialize(x,y) @x, @y = x, y end def *(c) Point.new(@x * c, @y * c) end end point = Point.new(1,2) p point p point * 3 Output: #<Point:0x336094 @x=1, @y=2> #<Point:0x335fa4 @x=3, @y=6> but then, 3 * point is not understood: Point can't be coerced into Fixnum ( TypeError ) So we need to further define an instance method coerce : class Point def coerce(something) [self, something]

Why is `'\t\n ' == false` in JavaScript?

巧了我就是萌 提交于 2019-12-23 10:44:11
问题 In JavaScript... '\t\n ' == false // true I can assume that any string that consists solely of whitespace characters is considered equal to false in JavaScript. According to this article, I figured that false would be converted to 0 , but was unable to find mention of whitespace considered equal to false using Google. Why is this? Is there some good reading on the subject besides digging into the ECMAScript spec? 回答1: This page provides a good summary of the rules. Going by those rules, the '

Question about jQuery source == on window

泪湿孤枕 提交于 2019-12-20 02:05:10
问题 data: function( elem, name, data ) { if ( !jQuery.acceptData( elem ) ) { return; } elem = elem == window ? windowData : elem; Copied directly from the jQuery source. Why is it not safe to use elem === window ? Why does jQuery use type coercion on the window object? It would appear that in IE there's an issue with top top == window // true top === window // false 回答1: See here for why checking againts the window object with === is unsafe in IE. I think the root cause is that IE is closely

nested runtime coercion for a Nullable double

萝らか妹 提交于 2019-12-13 16:05:31
问题 Let's say I have a value defined as a sort of commission formula let address_commission = 1.0 // minimal simplified example and I want to apply the above said commission to an amount I'm reading from the DB (the code is from a window WCF service I have in production) let address_commission = 1.0 // minimal simplified example new Model.ClaimModel( //RequestRow = i, recounting Code = (row.["claim_code"] :?> string), EvtDate = (row.["event_date"] :?> DateTime), // skipping lines... Amount = (row

The Less-than-or-equal Operator: With NaN

老子叫甜甜 提交于 2019-12-11 17:08:31
问题 When we use The Less-than-or-equal Operator this is work under the hood with The Abstract Relational Comparison Algorithm. For example. a <= b; Convert to JavaScript like this !(b < a) And EcmaScript Spesification says (http://www.ecma-international.org/ecma-262/5.1/#sec-11.8.5) which indicates that at least one operand is NaN less than return undefined And this is meaning var a = 1; var b = "asd" a < b // b.toNumber() => NaN and this is operation return undefined (false) If we use like this