Closed ghost closed 11 years ago
when I put constructor to end of class, then its not work. functions is reordered and constructor is placed after first function, not to end
The main bug is fixed. I had a look at the hasOwnProperty issue and remembered why I didn't use it before. Basically it doesn't do anything useful.
Look at the ClassExample test in https://github.com/Danack/PHP-to-Javascript/tree/hasOwnProperty. The code:
class TestClass {
public $var1 = 1;
public $var2 = 2;
function foo() {
}
}
$testClass = new TestClass();
foreach($testClass as $key => $objectValue) {
echo "value= ".$objectValue;
}
gets converted to
function TestClass() {
this.foo = function () {
};
}
TestClass.prototype.var1 = 1;
TestClass.prototype.var2 = 2;
var testClass = new TestClass();
for (var key in testClass) {
if (!testClass.hasOwnProperty(key)) continue;
var objectValue = testClass[key];
alert( "value= " + "" + objectValue);
}
Because the function is defined as a property on the class, hasOwnProperty returns true. The correct way to to avoid iterating over methods is to either just pass pure arrays, or be smarter with how methods are attached to objects: http://phrogz.net/death-to-hasownproperty
If you can think of a good example where hasOwnProperty would be useful, please open it as a separate issue.
and this is next thing what is wrong in your js pattern https://github.com/Danack/PHP-to-Javascript/issues/51#issuecomment-25185433 . when you define foo in prototype then its work.and you now know, you cant define property in prototype #35 . in php you can do this
class Foo2{
public $foo=5;
public function foo(){}
}
$foo=new Foo2();
foreach($foo as $nam=>$val){
echo $nam.'-'.$val.PHP_EOL;
}
will show "foo-5"
this throw js error. because this.foo is not exist in constructor; when I put constructor to end of class, then its work.