在php脚本中使用Javascript原型

在php脚本中使用Javascript原型,javascript,php,prototype,Javascript,Php,Prototype,我有一个javascript原型: function Shape(width, height){ this.width = width || 0; this.height = height || 0; } Shape.prototype.draw = function() { //... }; 还有一个php脚本,用于打印javascript代码: print "<script>\r\n" ."var x = function(){\r\n"

我有一个javascript原型:

function Shape(width, height){
  this.width = width || 0;
  this.height = height || 0; 
}

Shape.prototype.draw = function() {
    //...
};
还有一个php脚本,用于打印javascript代码:

print "<script>\r\n"
    ."var x = function(){\r\n"
        ."var r = new Shape(100, 100);\r\n"
        ."r.draw();\r\n"
    ."};\r\n"
    ."x();\r\n"
    ."x = undefined;\r\n"
    ."</script>";
但是生成的脚本代码不起作用。当我在生成的函数中使用警报消息而不是此消息时,将显示该消息。那么,我做错了什么,或者怎样才能让它工作呢

谢谢你的回答

对不起,我有点太不准确了。我想,有些信息并不重要。因此,我的原型声明在一个外部javascript文件中。
现在我找到了解决问题的方法:我将生成的脚本写入一个新的javascript文件,并将该文件绑定到主html中。然后我必须将一个局部变量设置为全局变量。。这是主要问题…*当我使用以下内容创建test.php时,代码运行良好:

<script>function Shape(width, height){
  this.width = width || 0;
  this.height = height || 0; 
}

Shape.prototype.draw = function() {
alert('Test');
};</script>
<?php
print "<script>\r\n"
    ."var x = function(){\r\n"
        ."var r = new Shape(100, 100);\r\n"
        ."r.draw();\r\n"
    ."};\r\n"
    ."x();\r\n"
    ."x = undefined;\r\n"
    ."</script>";?>

这是从PHP执行javascript代码的方法:

<?
//PHP code...
?>

<script type="text/javascript">
var x = function()
{
    var r = new Shape(100, 100);
    r.draw();
};

x();
</script>

<?
//More PHP
?>

如果您只是让php呈现javascript,而不是捏造字符串并打印它,您将有一个轻松得多的时间:例如。。。如果有条件地打印不同的值。否则,只需关闭php标签并正常编写脚本。是的,对不起,这是我的一个坏例子。我的原稿有很多行。但我找到了一个解决方案,请参见线程顶部。