javascript中的空白输出

javascript中的空白输出,javascript,Javascript,Helo friends我已经编写了一个js脚本来找出函数中的值 function hello(this) { this.value = 10; document.write(this); }; var c = new hello(c); c(); 但是我得到的输出是空的…我知道这取决于函数的调用方式,这里c是它的值…所以输出必须是c 我想在这里更改newhello(对任何字符串),还是像我一样更改c 请帮我解决这个问题并纠正我的错误…任何帮助都将不胜感激..谢谢..首先,您不能在java

Helo friends我已经编写了一个js脚本来找出函数中的值

function hello(this) {
this.value = 10;
document.write(this);
};


var c = new hello(c);
c();
但是我得到的输出是空的…我知道这取决于函数的调用方式,这里c是它的值…所以输出必须是c

我想在这里更改
newhello(对任何字符串)
,还是像我一样更改c


请帮我解决这个问题并纠正我的错误…任何帮助都将不胜感激..谢谢..

首先,您不能在javascript中使用“this”作为变量,因为它是一个保留关键字,您最好使用其他关键字。第二,如果要将字符串传递给函数,必须将其括在单(')引号或双引号(“)之间。第三,函数不返回任何值,因此无需将其指定给变量。第四,将10指定给“ths”中的属性“对象,但如果你知道我的意思,你返回的不是这个值。这是我的评论。您的代码更正如下:

function hello(ths) {
document.write(ths);
};

hello("your string goes here");
尝试:


框架结构,坚固。我猜你在找这样的事

function hello ()
{
    // inside a function (soon to be object) "this" refers to that object
    this.value = 10; // can and must be called by c.value outside of the declared function

    this.sayHello = function( msg )
    {
        this.value = msg; // is equal to c.value (outside of hello), overwrites old value with new message
        document.write( msg );
    }
}

// 'new' prefix types an object, represented by 'c'
var c = new hello;

c.sayHello( 'your message' );
// Your most recent message is stored in c.value

. 说真的,能够自己调试代码和了解语言本身一样重要。当我运行您的代码时,我得到错误
SyntaxError:missing formal parameter
。提示:不能用名称
this
声明变量或参数,并且
c
不是函数。我也不认为
c
能像你期望的那样工作。不要把
this
用作参数。用一个更好的名字<代码>此表示以下内容:。还有,为什么要将
c
传递到构造函数中,构造函数就是变量?因此,我们可以调用类似hello('anyvalue')。。对吧?以上都是。更不用说你真的不应该使用
document.write
。至少自1997年以来,我们有了更好的方法。是的,你可以用任何你想要的值调用任何函数。这是否有意义是另一个问题。请参阅以了解函数是如何工作的。如前所述。使用document.write函数在浏览器上显示某些内容并不实际。我建议您学习JQuery,它比DOM更容易处理该任务,DOM是旧方法。您可以随时编辑答案并将其添加到那里,而不是添加为注释。。
function hello ()
{
    // inside a function (soon to be object) "this" refers to that object
    this.value = 10; // can and must be called by c.value outside of the declared function

    this.sayHello = function( msg )
    {
        this.value = msg; // is equal to c.value (outside of hello), overwrites old value with new message
        document.write( msg );
    }
}

// 'new' prefix types an object, represented by 'c'
var c = new hello;

c.sayHello( 'your message' );
// Your most recent message is stored in c.value