Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/443.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
基本Javascript编程_Javascript - Fatal编程技术网

基本Javascript编程

基本Javascript编程,javascript,Javascript,我是JS编程新手,我想知道下面的代码怎么了 我就是不能让它显示任何东西 <!DOCTYPE html> <html> <body> <p>Creating and using an object method.</p> <p>An object method is a function definition, stored as a property value.</p> <p><a href=

我是JS编程新手,我想知道下面的代码怎么了

我就是不能让它显示任何东西

<!DOCTYPE html>
<html>
<body>
<p>Creating and using an object method.</p>
<p>An object method is a function definition, stored as a property value.</p>
<p><a href="#" onClick="typemodelcolour();">Click here</a></p>
<script>
var car = {
    type: "BMW",
    model: "350",
    colour: "Grey",
    typemodelcolour: function() {
        alert("Your car is a " + this.colour + " " + this.type + " " + this.model)
    };
};
</script>
</body>
</html>
我确信这是了解我的最基本的东西:

您创建了一个对象car,或者有时称为hash,它具有type函数的属性typemodelcolour

现在您已经定义了函数,需要调用它:

car.typemodelcolour();

请使用类似于驼峰符号的符号使代码可读:car.typeModelColour而不是car.typeModelColour

您错误地调用了方法。由于已将该方法声明为car对象中的对象,因此必须将其作为该car对象的属性进行访问:

事实上,应该是:

onclick="car.typemodelcolour()"
正如其他回答者所指出的,另一个错误的地方是在方法的定义中包含多余的分号。你有:

function(){alert("Your car is a " + this.colour + " " + this.type + " " + this.model)};
展开后,看起来有点像这样:

function() {
    alert("Your car is a " + this.colour + " " + this.type + " " + this.model)
};
在Javascript中,用分号终止带括号的语句没有任何意义,因此没有必要:

function(){alert("Your car is a " + this.colour + " " + this.type + " " + this.model)}
在javascript对象中,它们不应该有分号

当你叫它时

功能类型模式颜色{ var类型=宝马; var模型=350; 颜色=灰色; 提醒您的汽车是+类型++型号++颜色 } 创建和使用对象方法

对象方法是存储为属性值的函数定义


还有一个语法错误,使用;定义方法后。使用开发人员控制台。
function(){alert("Your car is a " + this.colour + " " + this.type + " " + this.model)}
var car = {
    type  : "BMW",
    model : "350",
    colour: "Grey",
    typemodelcolour: function(){alert("Your car is a " + this.colour + " " + 
this.type + " " + this.model)} //<== Remove the Semi colon
};
onclick="car.typemodelcolour()"