0

I have never seen this error in my life

weird error?

And I just found it so odd...

My code here is seen like this:

const no = require('noparenth');

(at index.js:1)

and I have literally never seen this in my life...


the library in question is one that just allows you to invoke a function without parenthesis. I made it so it would make it easier to code + make it faster. the code is as seen here:

Function.prototype.valueOf = function() {
    this.call(this);
    return 0; 
};

Thats it...

actual error:

C:\Users\summit\testing\index.js:1
��c


SyntaxError: Invalid or unexpected token
  • 1
    Looks like you have some randomish bytes at the start of that file. You could just delete it and re-type a new one. – Jared Smith Aug 30 '22 at 23:39
  • What if you want to pass a function, say as a callback...? Easier to code, let alone faster? I doubt it. –  Aug 30 '22 at 23:43
  • @ChrisG well I am not calling a new function, and if I was gonna do that i would might as well make the parentheses.. – summit tech Aug 30 '22 at 23:47
  • @JaredSmith I just did that, and it didnt work. – summit tech Aug 30 '22 at 23:49
  • 1
    You're going to have to debug this yourself. Nothing you've posted would cause that problem. Does node work with other files? If it does, then remove lines one a time until you find what line causes the problem. If not, then your node installation is corrupt somehow. Play with it, find what things make it behave differently, collect clues. If you're still stuck, update your question with the data you found. Otherwise, there's not much we can do to help you here. – Alex Wayne Aug 30 '22 at 23:54
  • 3
    Also, _"invoke a function without parenthesis"_. I'm 100% sure this is a _really_ bad idea. Adding a getter function for properties on some your objects, sure, but doing this to the global `Function` prototype is likely break so... many... things... – Alex Wayne Aug 30 '22 at 23:56
  • 1
    I think you have a BOM (byte order mark) at the start of the file. – CherryDT Aug 31 '22 at 13:19

1 Answers1

3

One possible way to get that type of characters is to save a file as UTF-16 LE and load it as if it were UTF-8.
If you use PowerShell, it's easy to produce a file in UTF-16 LE encoding, for example if you run echo "" > index.js. If you open the resulting file in VSCode, you can see at the bottom right that it's UTF-16 LE.

Note that the resulting file is not really empty, and you can verify that by inputting require('fs').readFileSync('index.js') in the Node.js REPL. You'll get <Buffer ff fe 0d 00 0a 00> which, when interpreted in UTF-16 LE, consists of byte order mark (U+FEFF), carriage return (U+000D) and new line character (U+000A).

Even if you open that file with a text editor and replace everything with your text, the byte order mark will still be there (as long as you still save in UTF-16 LE). For example, if you save const x = 0 the buffer will become like ff fe 63 00 ..., notice that the ff fe still is there.

When a program attempts to load it as UTF-8, it will get <invalid character> <invalid character> c <null character> and so on. What you're seeing in the output is exactly from <invalid character> <invalid character> c (Unicode: U+FFFD U+FFFD U+0063), obviously not a valid token in Node.js.


As for your actual function, it will only be invoked when you have type coercions (like +myFunction or myFunction + "") or if you directly call it like myFunction.valueOf(). And I expect it to make it (slightly) slower, because it calls a user-defined function rather than just native code.

And apart from the bad idea of modifying prototypes of objects you don't own, there is limited usefulness (if any) to this.
The this.call(this) means that you can't add more arguments to the function call.
Also, it's like myFunction.call(myFunction), I'm not sure why you would want to do that. And when used on a method of an object, it's like myObject.myMethod.call(myObject.myMethod) (with this equal to the function itself) rather than myObject.myMethod.call(myObject)

And it will break things that depend on the behavior of myFunction + "" etc.
It seems not that easy to find an example, nevertheless I found this example : isNative(Math.sin) was originally supposed to return true, but after modifying the prototype the way shown above, it became 3

qrsngky
  • 2,263
  • 2
  • 13
  • 10