Das globale Object in JavaScript (Das globale Object in JavaScript), Lektion, Seite 723379
https://www.purl.org/stefan_ram/pub/global_dump_javascript (Permalink) ist die kanonische URI dieser Seite.
Stefan Ram
JavaScript-Kurs

Verschiedenes

UFilter

Unter einem „UFilter“ wird hier ein Programm verstanden, welches den für das Programm sichtbaren und erreichbaren Namensraum ausgibt.

Hier folgt ein UFilter für JavaScript.

Main.html

<!DOCTYPE HTML><html><head><meta charset="UTF-8">
<title>Main</title>
</head><body><pre><code>
<script>

function documentWrite( s )
{ document.write( s.
replace( /&/g, '&amp;' ).
replace( /"/g, '&quot;' ).
replace( /'/g, '&apos;' ).
replace( /</g, '&lt;' ).
replace( />/g, '&gt;' )); }

function emil( text ){ documentWrite( text ); document.write( "<br />" ); }

function emil0( text ){ console.log( text ); }

function dbemit0( text ){}

function explain( text, text1 ){ emil( "(" + text + "): " + text1 ); }

function mydir( path, arg, seen, desc )
{ if( path.includes( "TheEnumeratorHasAlreadySeenThisObject" ))return;
try
{ let t = typeof( arg );
let d = t === "object" ? Object.prototype.toString.call( arg ) : t;
if( !arg && t === "object" )
{ dbemit0( 0 ); emil( path + " = " + desc + "null;" ); }
else if( t === "undefined" )
{ dbemit0( 1 ); emil( path + " = " + desc + "undefined;" ); }
else if
( arg.hasOwnProperty( "TheEnumeratorHasAlreadySeenThisObject" ))
{ dbemit0( "2a" );
try{ emil( path + " = " + desc + arg.getOwnProperty( "TheEnumeratorHasAlreadySeenThisObject" )); }
catch( exception )
{ if( seen.get( arg ) )
{ emil( path + " = " + desc + seen.get( arg )); }
else
{ emil( path + " = " + desc + "(S)" ); }}}
else if( seen.get( arg ) )
{ dbemit0( "2b" ); emil( path + " = " + desc + seen.get( arg )); }
else
{ let arg1 = 0;
switch( t )
{ case "function":
dbemit0( 5 );
case "object":
try { seen.set( arg, path ); }
catch( exception ){}
try { arg.TheEnumeratorHasAlreadySeenThisObject = path; }
catch( exception ){}
if( t === "object" ){ dbemit0( 3 ); emil( path + " = " + desc + d ); }
if( t === "function" ){ emil( path + " = " + desc + "[object (" + d + ")] : name = " +( arg.name ? arg.name : "(unnamed)" )); }
Object.getOwnPropertyNames( arg ).forEach
( function( key, idx, array )
{ try
{ let arg1 = arg[ key ];
let textual = "???";
try
{ let descriptor = Object.getOwnPropertyDescriptor( arg, key );
textual =
( descriptor.enumerable ? "e" : "E" )+
( descriptor.writable ? "w" : "W" )+
( descriptor.configurable ? "c" : "C" );
}
catch( exception ){}
try { mydir( path + "." + key, arg1, seen, "(" + textual + ") " ); } catch( exception )
{ dbemit0( "4xb" ); emil( path + "." + key + " = " + desc + "(E)" ); }
} catch( exception ) { dbemit0( "4xb" ); emil( path + "." + key + " = " + desc + "(E)" ); }});
mydir( path + ".(P)", Object.getPrototypeOf( arg ), seen, "" );
break;
default:
dbemit0( 6 );
emil( path + " = " + desc + d + " : " + arg ); break; }}}
catch( exception )
{ dbemit0( "7" ); emil( "exception: " + exception + exception.lineNumber + exception.ColumnNumber ); }};

explain( "G", "the global object" );
explain( "P", "the prototype of the object whose path is preceding .(P)" );
explain( "E", "an error has occured trying to display this" );
explain( "S", "object was probably seen, but the path is not available" );
explain( "EWc", "property is configurable, but not enumerable or writable" );
emil( "" );
mydir( "(G)", this, new WeakMap(), "" );

</script></code></pre></body></html>

Parser

Main.html

<!DOCTYPE HTML><html><head><title>Main</title>
<meta charset="UTF-8"></head><body><pre><code><script>

/* The Scanner has to get the next token from the input.
One can call "check" to try and get a token specified by a
pattern or "numeral" to get a numeral. */

function Scanner( source ){ this.source = source; this.pos = 0; };
Scanner.prototype =
{ check: function( pattern )
{ var next = this.source.substring( this.pos, this.pos + 1 );
if( pattern.exec( next )){ ++this.pos; return next }
else return false; },
numeral: function()
{ var next = /[0-9]+/.exec( this.source.substring( this.pos ));
this.pos += next[ 0 ].length; return next[ 0 ]; }, };

/*
Information about operators is kept in two tables:
"ex" = how to execute an operation
"left_assocative" = whether an operation is left-associative */


var ex = Object.create( null );
ex[ "^" ]= Math.pow;
ex[ "*" ]= function( a, b ){ return a * b };
ex[ "/" ]= function( a, b ){ return a / b };
ex[ "+" ]= function( a, b ){ return a*1 + b*1 };
ex[ "-" ]= function( a, b ){ return a - b };

var left_associative = Object.create( null );
left_associative[ "^" ]= false;
left_associative[ "*" ]= true;
left_associative[ "/" ]= true;
left_associative[ "+" ]= true;
left_associative[ "-" ]= true;

/*
After these preparations, it's easy to write the parser to
detect some common symbols, such as numerals, primaries,
powers, products, sums, and start. Common code for binary
operators is kept in "binop", which, in a sense, is the
- powers, products, sums, and start. Common code for binary
operators is kept in "binop", which, in a sense, is the
heart of the whole parser.

The parameter "next" gives the type of the operands, e.g.,
sum ::= product + product, so for the sum, "next" is "product".

The code of "binop" is quite compact, unifying code for
left- and right-associative operations. To understand it,
split it into an if with separate branches for each case.
You might even write it out for each operator separately.

Exercises: Add function calls. Add error reporting and
recovery. Extend it into you personal programming language.
*/



function Parser( source ){ this.scanner = new Scanner( source ); };
Parser.prototype =
{ numeral: function( me ){ return me.scanner.numeral(); },
primary: function( me ){ /* check f ( here */ return me.numeral(); },
binop: function( me, op, next )
{ var result = next( me ); var sym;
while( sym = me.scanner.check( op ))
result = ex[ sym ]( result,
( left_associative[ sym ]? next( me ): me.binop( me, op, next )));
return result; },
power: function( me ){ return me.binop( me, /[\^]/, me.primary ); },
product: function( me ){ return me.binop( me, /[*\/]/, me.power ); },
sum: function( me ){ return me.binop( me, /[+-]/, me.product ); },
start: function(){ return this.sum( this ); }};

function evl( x ){ var parser = new Parser( x ); return parser.start(); }

function check( x, y )
{ var e = evl( x ); var r = e == y*1;
document.writeln( x + " = " + e + " == " + y + "; // " + r ); }

check( "0", "0" );
check( "1", "1" );
check( "11", "11" );
check( "1+1", "2" );
check( "11+1", "12" );
check( "2^3", "8" );
check( "2^3-1", "7" );
check( "2^3/2", "4" );
check( "2^3^2", "512" );
check( "6+3*2^3+1", "31" );

</script></code></pre></body></html>

Proxy

main.js
var b = new Proxy( {}, { get: function( target, name )
{ return name === "f" ? function(){ return "hu!"; } : b; }});
print( b[ 43 ][ 201 ][ -119 ][ +1023 ][ 72 ].f() );
Protokoll
hu!
main.js

const b = new Proxy( {}, { get: function( target, name )

{ return name === "f" ? ()=>"hu!" : b; }});

print( b[ 43 ][ 201 ][ -119 ][ +1023 ][ 72 ].f() );

Protokoll

hu!

JavaScript

var alpha = new Proxy( {}, { get: function( target, name )

{ return name === "height" ? target.width * 2 : target[ name ]; }});

alpha.width = 100;

console.log( alpha.height );

Microsoft JScript

JavaScript

var ExcelApp = new this.ActiveXObject( "Excel.Application" );

var ExcelSheet = new this.ActiveXObject( "Excel.Sheet" );

var wscript = new this.ActiveXObject("WScript.Shell");

var xlToRight = -4161

ExcelSheet.Application.Visible = true;

ExcelSheet.ActiveSheet.Cells( 1, 1 ).Value = "This is column A, row 1";

ExcelSheet.ActiveSheet.Cells( 1, 2 ).Value = "This is column B, row 1";

ExcelSheet.ActiveSheet.Cells( 1, 3 ).Value = "This is column C, row 1";

ExcelSheet.ActiveSheet.Cells( 1, 4 ).Value = "This is column D, row 1";

ExcelSheet.ActiveSheet.Cells( 1, 5 ).Value = "This is column E, row 1";

ExcelSheet.ActiveSheet.Cells( 1, 6 ).Value = "This is column F, row 1";

WSH.echo

( "The extension is " +

ExcelSheet.ActiveSheet.Cells( 1, 1 ).End( xlToRight ).Column );

ExcelSheet.Application.Quit();

Berechnung der 100000. Fibonacci-Zahl

JavaScript

{ const alpha = new Date().getTime();
const fib =
( () =>
{ "use strict";
const e = 14;
const b = eval( "1e" + e );
const f = Array( e ).join( "0" );
let x = new Float64Array( 3e4 ); x[ 0 ] = 1.0; let xl = 1;
let r = new Float64Array( 3e4 ); r[ 0 ] = 1.0; let rl = 1;
let R = new Float64Array( 3e4 ); let Rl = 0;
let j = new Array( 3e4 );
let s = 0;
let a; let al;
let o = 0;
let p = 0;
let g; let G;
let h; let H;

const fibonacci =( n )=>
{ for( let i = 1; i < n - 1; ++i )
{ if( rl < xl ){ g = r; G = rl; h = x; H = xl; }
else { g = x; G = xl; h = r; H = rl; }
p = 0; o = 0;
while(p<G){s=o+g[p]+h[p];R[p++]=(s-(o=+(s>b))*b);}
while(p<H){s=o+h[p];R[p++]=(s-(o=+(s>b))*b);}
if(o!=0){s=o;R[p++]=(s-(o=+(s>b))*b);}
Rl = p;
a = R; al = Rl; R = x; Rl = xl; x = r; xl = rl; r = a; rl = al; }
let i = 0;
for( ; i < al - 1; ++i )j[ i ]=( f +( a[ i ] )).slice( -e );
j[ i ]= a[ i ];
return j.slice( 0, al ); }

return fibonacci( 100000 ).reverse().join( "" ); } )()

const omega = new Date().getTime();
console.log( fib );
console.log( omega - alpha ); }

Seiteninformationen und Impressum   |   Mitteilungsformular  |   "ram@zedat.fu-berlin.de" (ohne die Anführungszeichen) ist die Netzpostadresse von Stefan Ram.   |   Eine Verbindung zur Stefan-Ram-Startseite befindet sich oben auf dieser Seite hinter dem Text "Stefan Ram".)  |   Der Urheber dieses Textes ist Stefan Ram. Alle Rechte sind vorbehalten. Diese Seite ist eine Veröffentlichung von Stefan Ram. Schlüsselwörter zu dieser Seite/relevant keywords describing this page: Stefan Ram Berlin slrprd slrprd stefanramberlin spellched stefanram723379 stefan_ram:723379 Das globale Object in JavaScript Stefan Ram, Berlin, and, or, near, uni, online, slrprd, slrprdqxx, slrprddoc, slrprd723379, slrprddef723379, PbclevtugFgrsnaEnz Erklärung, Beschreibung, Info, Information, Hinweis,

Der Urheber dieses Textes ist Stefan Ram. Alle Rechte sind vorbehalten. Diese Seite ist eine Veröffentlichung von Stefan Ram.
https://www.purl.org/stefan_ram/pub/global_dump_javascript