Function java scrip对象don';t调用其中的函数

Function java scrip对象don';t调用其中的函数,function,class,logging,console,Function,Class,Logging,Console,这个代码怎么了?我想在类内的函数中将消息打印到cnsole中 function Clas(x); { this.x = x; function nothing() { console.log(x); } } var clas = new Clas(1); clas.nothing(); nothing()未公开。您需要将其附加到此 function Something(x) { this.x = x; this.nothing = function() { con

这个代码怎么了?我想在类内的函数中将消息打印到cnsole中

function Clas(x);
{
   this.x = x;
function nothing()
{
    console.log(x);
}
}
var clas = new Clas(1);
clas.nothing();
nothing()
未公开。您需要将其附加到

function Something(x) {
  this.x = x;
  this.nothing = function() {
    console.log(this.x);
  }
}

var something = new Something(3);
something.nothing(); // 3
想要这样的吗? 您可以返回包含函数的
JSON
对象。(所以它有点像OOP。)

这是小提琴:

function Clas(x) {
    return {
        x : x,
        nothing : function () {
            console.log(x);
        }
    }
}

var clas = new Clas(1);
clas.nothing();