从对象访问属性/方法(javascript)
我是一个具有传统OOPs知识的典型Java程序员。我现在正在努力学习JS。我了解到,函数是JS,它是一类对象,与其他任何对象一样,它具有属性和方法。基于此,我编写了以下代码:从对象访问属性/方法(javascript),javascript,oop,Javascript,Oop,我是一个具有传统OOPs知识的典型Java程序员。我现在正在努力学习JS。我了解到,函数是JS,它是一类对象,与其他任何对象一样,它具有属性和方法。基于此,我编写了以下代码: function Note(name, text) { this.name = name; this.text = text; function displayNote() { alert(this.text); } } var note = new Note("a"
function Note(name, text)
{
this.name = name;
this.text = text;
function displayNote()
{
alert(this.text);
}
}
var note = new Note("a", "c");
alert(note.displayNote);
我现在尝试从not对象访问displayNote函数,代码如下:
function Note(name, text)
{
this.name = name;
this.text = text;
function displayNote()
{
alert(this.text);
}
}
var note = new Note("a", "c");
alert(note.displayNote);
但它没有定义。这可能是由于范围问题。所以我试着用这个:
function Note(name, text)
{
this.name = name;
this.text = text;
function displayNote()
{
var self = this;
alert(self.text);
}
}
var note = new Note("a", "c");
alert(note.displayNote);
还是一样的结果。有什么解释吗?试试这个
this.displayNote = function()
{
alert(this.text);
}
现在它是Note
对象的一个属性。另外,我认为您希望这样做:
note.displayNote(); // Note the brackets to call.
试试这个
this.displayNote = function()
{
alert(this.text);
}
现在它是Note
对象的一个属性。另外,我认为您希望这样做:
note.displayNote(); // Note the brackets to call.
试试这个
this.displayNote = function()
{
alert(this.text);
}
现在它是Note
对象的一个属性。另外,我认为您希望这样做:
note.displayNote(); // Note the brackets to call.
试试这个
this.displayNote = function()
{
alert(this.text);
}
现在它是Note
对象的一个属性。另外,我认为您希望这样做:
note.displayNote(); // Note the brackets to call.
您需要执行以下操作:
function Note(name, text)
{
this.name = name;
this.text = text;
this.displayNote = function()
{
alert(this.text);
}
}
var note = new Note("a", "c");
note.displayNote();// it will automatically alert. Otherwise you'll have two alerts. The second one being undefined.
它显示为未定义,因为您尚未定义displayNote
属性
此外,要调用函数,您需要执行以下操作:
function Note(name, text)
{
this.name = name;
this.text = text;
this.displayNote = function()
{
alert(this.text);
}
}
var note = new Note("a", "c");
note.displayNote();// it will automatically alert. Otherwise you'll have two alerts. The second one being undefined.
您需要执行以下操作:
function Note(name, text)
{
this.name = name;
this.text = text;
this.displayNote = function()
{
alert(this.text);
}
}
var note = new Note("a", "c");
note.displayNote();// it will automatically alert. Otherwise you'll have two alerts. The second one being undefined.
它显示为未定义,因为您尚未定义displayNote
属性
此外,要调用函数,您需要执行以下操作:
function Note(name, text)
{
this.name = name;
this.text = text;
this.displayNote = function()
{
alert(this.text);
}
}
var note = new Note("a", "c");
note.displayNote();// it will automatically alert. Otherwise you'll have two alerts. The second one being undefined.
您需要执行以下操作:
function Note(name, text)
{
this.name = name;
this.text = text;
this.displayNote = function()
{
alert(this.text);
}
}
var note = new Note("a", "c");
note.displayNote();// it will automatically alert. Otherwise you'll have two alerts. The second one being undefined.
它显示为未定义,因为您尚未定义displayNote
属性
此外,要调用函数,您需要执行以下操作:
function Note(name, text)
{
this.name = name;
this.text = text;
this.displayNote = function()
{
alert(this.text);
}
}
var note = new Note("a", "c");
note.displayNote();// it will automatically alert. Otherwise you'll have two alerts. The second one being undefined.
您需要执行以下操作:
function Note(name, text)
{
this.name = name;
this.text = text;
this.displayNote = function()
{
alert(this.text);
}
}
var note = new Note("a", "c");
note.displayNote();// it will automatically alert. Otherwise you'll have two alerts. The second one being undefined.
它显示为未定义,因为您尚未定义displayNote
属性
此外,要调用函数,您需要执行以下操作:
function Note(name, text)
{
this.name = name;
this.text = text;
this.displayNote = function()
{
alert(this.text);
}
}
var note = new Note("a", "c");
note.displayNote();// it will automatically alert. Otherwise you'll have two alerts. The second one being undefined.
在这里,代码如下
alert(note.displayNote);
您正在调用警报两次
所以请调用函数
note.displayNote();
在这里,代码为
alert(note.displayNote);
您正在调用警报两次
所以请调用函数
note.displayNote();
在这里,代码为
alert(note.displayNote);
您正在调用警报两次
所以请调用函数
note.displayNote();
在这里,代码为
alert(note.displayNote);
您正在调用警报两次
所以请调用函数
note.displayNote();
你可以像下面这样使用
<script language="javascript" type="text/javascript">
<!--
person = new Object()
person.name = "Tim Scarfe"
person.height = "6Ft"
person.run = function() {
this.state = "running"
this.speed = "4ms^-1"
}
//-->
</script>
您可以像下面这样使用
<script language="javascript" type="text/javascript">
<!--
person = new Object()
person.name = "Tim Scarfe"
person.height = "6Ft"
person.run = function() {
this.state = "running"
this.speed = "4ms^-1"
}
//-->
</script>
您可以像下面这样使用
<script language="javascript" type="text/javascript">
<!--
person = new Object()
person.name = "Tim Scarfe"
person.height = "6Ft"
person.run = function() {
this.state = "running"
this.speed = "4ms^-1"
}
//-->
</script>
您可以像下面这样使用
<script language="javascript" type="text/javascript">
<!--
person = new Object()
person.name = "Tim Scarfe"
person.height = "6Ft"
person.run = function() {
this.state = "running"
this.speed = "4ms^-1"
}
//-->
</script>
此链接可能有助于了解javascript中的闭包概念此链接可能有助于了解javascript中的闭包概念此链接可能有助于了解javascript中的闭包概念此链接可能有助于了解javascript中的闭包概念