In javascript, how to read the specified data of the specified line of the file
In Javascript, how to read the specified data of the specified line of the file The following code only tests the reading of the specified line. It is feasible to test using .hta. The core code is as follows: function readLine(){ var line=document.all(“line”).value||0 //Getting the specified line number defaults to starting from the first line 0 f = fso. OpenTextFile(“D:\\txt.txt”, ForReading); for(var i=0;i<line;i++){ f.SkipLine(); //Skip the specified line } r=f.ReadLine(); //Read the target line document.all("txtBox").innerText=r //Output result } How to use JS to read a TXT file line by line? Solution: 1. Through the FSO method of IE, but this method can only be used in IE 2. Read text through Ajax. This method is universal and it is recommended to use this method. Code example: $(document).ready(function(){ $(“#b01″).click(function(){//For example, in the click event of the button htmlobj=$.ajax({url:”/jquery/test1. txt”,async:false});//Read the test1.txt text file through ajax. Can Javascript read the content of the txt file? Yes! The first step : Create an object that can translate files into file streams. Var fso=new ActiveXObject(Scripting.FileSystemObject); Step 2: Used to create a textStream object. There are three attributes in brackets 1. The absolute path of the file 2. The file’s Constant read only=1, write only=2,…
In Javascript, what exactly does a shift operation with a right operand above 31 do?
I know that the operands of bitwise operations are treated as 32-bit integers in Javascript. So I thought if I <<32 adjust the integer value it will go to zero. But it works It’s like <<0. Why does it work this way? In Javascript, what exactly does a shift operation with a right operand above 31 do? 0b00000000000000000000000000000001 <<32 // 1 0b00000000000000000000000000000001 <<33 // 2 0b00000000000000000000000000000001 <> 30 // 0 0b00000000000000000000000000000001 >> 31 // 0 // Something weird is happening…. 0b00000000000000000000000000000001 >> 32 // 1 0b00000000000000000000000000000001 >> 33 // 0 Herohtar.. 9 It truncates the binary representation of the right operand. From the documentation for the Bitwise Shift operator: The shift operator converts its operands to 32-bit integers in big-endian order and returns a result of the same type as the left operand.The right operand should be less than 32, but if not, just Low 5 digits. So in your example, 32 is 0b100000. Since it only has the lowest five digits you have left, 0b00000, so It shifts the number by 0. Again, 33 is 0b100001 and the lowest five digits are 0b00001, so it shifts the number Shift 1.etc. 1> Herohtar..: It truncates the right Binary representation of…
In JavaScript, null>=0 is true, but null==0 is false. What exactly is null?
1. Preface Today I saw friends discussing an issue, asking whether null is equal to 0. After hearing this, I quickly wrote a demo to try it out. <html lang=”en”><head> < meta charset=”UTF-8″ > <title>MR_LP:3206064928</title></head ><body></body><script type=”text/Javascript”> console.log(null > 0); // false console.log(null <0); // false console.log(null >= 0); // true console.log(null <= 0); // true console.log(null == 0); // false console .log(null === 0); // false</script></html> What is the situation? Why are the judgments of console.log(null <= 0); and console.log(null >= 0); true? 2. Check data If we want to know exactly what this problem is, then we need to review our ECMAScript Language Specification (HTML version), which translates to ECMAScript language Specification (HTML version). 2.1 Internal Equality Operation Algorithm First, let’s take a look at ES3’s algorithm implementation of internal equality operation. 11.9.3 The Abstract Equality Comparison Algorithm The comparison x == y, where x and y are values, produces true or false. Such a comparison is performed as follows: 1. If Type(x) is different from Type(y), go to step 14. 2. If Type(x) is Undefined, return true. 3. If Type(x) is Null, return true. 4. If Type(x) is not Number , go to step 11. 5. If…
What are the differences between “=, ==, ===” in JavaScript?
This article mainly introduces the differences between “=, ==, ===” in Javascript. The introduction in the article is very detailed and has certain reference value. Interested friends must read it Finished reading! = is an assignment operation, == is used for general comparison, and === is used for strict comparison. == Data types can be converted during comparison. ; === Strict comparison, returns false as long as the types do not match. Example: “1” == true The types are different, “==” will perform type conversion first, Convert true to 1, which is “1” == 1; At this time, the types are still different, continue to perform type conversion, and convert “1” to 1, which is 1 == 1; At this time, the types on the left and right sides of “==” are both numerical, which is quite successful! If comparing: “1” === true, the left side is character type, and the right side is bool type. If the left and right sides have different types, the result is false; If comparing: “1” === 1 The left side is character type, the right side is int numeric type, the left and right sides have different types, the result is false; If…
In JavaScript, the object itself is a Map structure.
var map = {}; map[ ‘key1’ ] = 1; map[ ‘key2@’ ] = 2; console.log(map[ ‘key1’ ]); //The result is 1. console.log(map[ ‘key2@’ ]); //The result is 2. //If traversing map for ( var prop in map){ if (map.hasOwnProperty(prop)){ console.log( ‘key is ‘ + prop + ‘ and value is’ + map[prop]); } }
In JavaScript, what does the last bracket of ”function({})(req,res)” mean?
I finally started serious software development again after a year and lost my Javascript reference book. Sorry if this is a stupid question, but I’ve forgotten what this closing bracket means in Javascript. p> Here is some sample code: login: function (req, res) { example.function(‘optionA’, function (err, user) { if (err) return res.send(err); return res.send(user); })(req, res); }); is(req,res) What confuses me is how exactly does this work? Thanks in advance. Feeling less like Albert Einstein now! 1> Frank Modica..: It looks like Like the example.function report function, you can see the call and pass in req and res. Like this: let func = example.function(‘optionA’, function (err, user) { if (err) return res.send(err); return res.send(user); }); func(req, res);
In JavaScript, variable i is defined, so how to get the value of ii?
This is a question raised after reading this code. function doz(h,sj) { var kk,hh; kk=h.split(“”); hh=h.split(“”).reverse(); mm=document.getElementById(“p”+h).innerHTML.split(” – “); var m = new Array(); for(i=0;i<mm.length;i++) { if(in_array(i,kk)==1) { m[hh[ii]]=mm[i]; } else { m[i]=mm[i]; } } document.getElementById(sj).innerHTML=m.join(” – “); } This code is taken from http://www.8684.cn/js/m.js Reference pages such as: http://beijing.8684.cn/x_0265ef34 I debugged it myself, but I didn’t understand the correspondence between ii and i! Could you please help me understand it? 10 solutions #1 I have never defined ii #2 Quoting the reply from varlj on the 1st floor: I have never defined ii Not found! You can use firebug to debug the page http://beijing.8684.cn/x_0265ef34. You can see that the value of ii is different every time it loops, so it feels strange. #3 Where is ii defined? Global??… Define global variables in functions? #4 I looked at the code of that website, I can’t believe my eyes, I have to admire the author of this website. I guess this expert must be practicing how to write the messiest and most difficult code in the world. The original code is excerpted as follows function in_array(i,kk) { for(ii=0;ii<kk.length;ii++) { if(i==kk[ii]) { return 1; } } } function doz(h,sj) { var kk,hh;…
In JavaScript, recursively build a dictionary/nested object from a set of arrays
I feel like I’m being a bit hypocritical, but I’m having a hard time finding a solution. I have a set of arrays that I need to use to build a JSON-ish object. For example [a] [a, b] [a, b, c] [a, b, d] [e] [e, f] [e, f, g] Change { a: { b: { c: {} d: {} } } e: { f: { g: {} } } } Wait. What I want to do is: Instantiate an empty object, Dictionary Take an arbitrary array of length n Iterate through the array so that at array position i, if Dictionary has no attribute in Dictionary [Array [0]] … [Array [i]], I define the attribute as Array [i]:{} The problem I’m having is viewing arbitrary paths to related properties. I don’t know how to construct a multi-level path to the property name I’m looking for. I.e. when I === 0, var check = Array[i]; typeof Dictionary[check] === ‘undefined’; We get the expected behavior. But it obviously builds the entire array as a set of flat object properties (rather than a nested dictionary). I have no way to add the next step in the check variable – … check =…
In JavaScript, what scenarios do different design patterns deal with?
Recently, I looked at the advanced design patterns of Javascript. There are many patterns in it, let’s say at least a dozen. But at present, I feel that single case + factory can write all the requirements, and there seem to be very few opportunities for other design patterns to appear. Some of the scenarios for using design patterns in the book are very abstract. After reading the examples, as mentioned before, use singleton + factory. It can also be solved. It’s hard to understand which mode to use in which scenario. Now it’s like cramming a lot of modes into your brain, which is very inefficient! Is there any article that briefly describes the characteristics of various design patterns and what scenarios they are suitable for?
In Javascript, why is the “this” operator inconsistent?
In Javascript, this always refers to the object that calls the function being executed. Therefore, if this function is used as an event handler, this will refer to the node that fired the event. However, if you have an object and call a function on it, for example: myobject.myFunction(); then <code myFunction in >this will refer to myobject. Does it make sense? To solve this problem, you need to use closures. You can change the code as follows: function TestObject() { TestObject.prototype.firstMethod = function(){ this.callback(); YAHOO. util.Connect.asyncRequest(method, uri, callBack); } var that = this; TestObject.prototype.callBack = function(o){ that.secondMethod(); } TestObject.prototype.secOndMethod= function() { alert(‘test’); }}