Joseph James Shenton Joseph James Shenton - 5 months ago 14
Javascript Question

Javascript variable variables unexpected token >

Before you say this is a dupelicate i have read all the other post and they kinda solved my question but now i need to know why the following comes up as an Uncaught SyntaxError: Unexpected token >
Below i include my whole library so you can see what i have coded in hope this would work please help me


php2js.$this->();


this is my library

php2js = window.php2js || {};

php2js = function () {

var yourVar1;
var yourVar2;

publicFunc1 = function(content) {
document.write(content);
}

publicFunc2 = function(handle, first, last) {
var string = handle.split(first);
if (1 in string) {
var output = string[1].split(last);
return output[0];
}
return '';
}

publicFunc3 = function(str,begin,end) {
t = str.split(begin);
t = t[1].split(end);
return t[0];
}

publicFunc4 = function(delimiter, string, limit) {
if (arguments.length < 2 ||
typeof delimiter === 'undefined' ||
typeof string === 'undefined') {
return null
}
if (delimiter === '' ||
delimiter === false ||
delimiter === null) {
return false
}
if (typeof delimiter === 'function' ||
typeof delimiter === 'object' ||
typeof string === 'function' ||
typeof string === 'object') {
return {
0: ''
}
}
if (delimiter === true) {
delimiter = '1'
}

delimiter += ''
string += ''

var s = string.split(delimiter)

if (typeof limit === 'undefined') return s

if (limit === 0) limit = 1

if (limit > 0) {
if (limit >= s.length) {
return s
}
return s
.slice(0, limit - 1)
.concat([s.slice(limit - 1)
.join(delimiter)
])
}

if (-limit >= s.length) {
return []
}

s.splice(s.length + limit)
return s
};

publicFunc5 = function(text, name, type) {
var a = document.createElement("a");
var file = new Blob([text], {type: type});
a.href = URL.createObjectURL(file);
a.download = name;
a.click();
}

publicFunc6 = function(id, string) {
var element = document.getElementById(id);
element.innerHTML = string;
}

publicFunc7 = function(grab) {
var parts = window.location.search.substr(1).split("&");
var $_GET = {};
for (var i = 0; i < parts.length; i++) {
var temp = parts[i].split("=");
$_GET[decodeURIComponent(temp[0])] = decodeURIComponent(temp[1]);
}

return $_GET[grab];
}

publicFunc8 = function(name,value,days) {
if (days) {
var date = new Date();
date.setTime(date.getTime()+(days*24*60*60*1000));
var expires = "; expires="+date.toGMTString();
}
else var expires = "";
document.cookie = name+"="+value+expires+"; path=/";
}

publicFunc9 = function(name) {
var value = "; " + document.cookie;
var parts = value.split("; " + name + "=");
if (parts.length == 2) return parts.pop().split(";").shift();
}

publicFunc10 = function() {
return navigator.userAgent;
}

publicFunc11 = function(str) {
return String(str).replace(/&/g, '&amp;').replace(/</g, '&lt;').replace(/>/g, '&gt;').replace(/"/g, '&quot;');
}

publicFunc12 = function() {
var obj = {}
obj.foo = 42;

var bar = 'foo';
console.log(obj[bar]);
}

return {
"echo" : publicFunc1,
"GetBetween" : publicFunc2,
"GBS" : publicFunc3,
"explode" : publicFunc4,
"fcd" : publicFunc5,
"echoById" : publicFunc6,
"$_GET" : publicFunc7,
"setCookie" : publicFunc8,
"$_COOKIE" : publicFunc9,
"$UA" : publicFunc10,
"htmlentities" : publicFunc11,
"$this->" : publicFunc12
}

}();

Answer

So you have an object with some methods stored in it.The keys have values that are not valid in the dot syntax. In order to call it with your code, you would need to use bracket notation. Basic idea:

var php2js = (function() {
  return {
    "$this->": function() {
      console.log("hey");
    }
  };
}());

php2js["$this->"]();