如何在一条语句中调用JavaScript对象方法(方法链接)
我在函数中创建了一个对象。我想在其中调用对象方法 声明如何在一条语句中调用JavaScript对象方法(方法链接),javascript,method-chaining,Javascript,Method Chaining,我在函数中创建了一个对象。我想在其中调用对象方法 声明 (函数(){ s4=功能(getSection){ var self={ 名称:函数(getname){ log(“学生姓名”+getname); }, 主题:功能(getsub){ console.log(“&studing”+getsub); } } 回归自我; } })(); 学生资料 学生资料 s4(“第五”)。姓名(“约翰”)//这个很好用 s4(“第五”)。主题(“Python”)//这个很好用 s4(“第五”)。名称(“约
(函数(){
s4=功能(getSection){
var self={
名称:函数(getname){
log(“学生姓名”+getname);
},
主题:功能(getsub){
console.log(“&studing”+getsub);
}
}
回归自我;
}
})();代码>
学生资料
学生资料
s4(“第五”)。姓名(“约翰”)//这个很好用
s4(“第五”)。主题(“Python”)//这个很好用
s4(“第五”)。名称(“约翰”)。主题(“Python”)//显示错误:SCRIPT5007:无法获取未定义或空引用的属性“subject”
在方法中,返回对象在名称和主题中返回此
。
这称为方法链接,它通过返回对象来工作
(函数(){
s4=功能(getSection){
var self={
名称:函数(getname){
log(“学生姓名”+getname);
归还这个;
},
主题:功能(getsub){
console.log(“&studing”+getsub);
归还这个;
}
}
回归自我;
}
})();
s4(“第五”)。姓名(“约翰”);
s4(“第五”)。主题(“Python”);
s4(“第五”)。名称(“约翰”)。主题(“Python”)代码>在方法中,返回对象在名称和主题中返回此
。
这称为方法链接,它通过返回对象来工作
(函数(){
s4=功能(getSection){
var self={
名称:函数(getname){
log(“学生姓名”+getname);
归还这个;
},
主题:功能(getsub){
console.log(“&studing”+getsub);
归还这个;
}
}
回归自我;
}
})();
s4(“第五”)。姓名(“约翰”);
s4(“第五”)。主题(“Python”);
s4(“第五”)。名称(“约翰”)。主题(“Python”)调用的代码>,通常通过使每个不返回其他值的函数返回该值来实现代码>:
name: function(getname){
console.log("Name of the Student " +getname);
return this;
},
subject: function(getsub){
console.log(" & Studying " +getsub);
return this;
}
然后s4(“第五”).name(“约翰”).subject(“Python”)
将起作用,因为name(“John”)
返回调用的s4(“第五”)
的结果,通常通过使每个不返回其他值的函数返回该值来实现代码>:
name: function(getname){
console.log("Name of the Student " +getname);
return this;
},
subject: function(getsub){
console.log(" & Studying " +getsub);
return this;
}
然后s4(“第五”).name(“约翰”).subject(“Python”)由于name(“John”)
返回s4(“第五”)
的结果,因此code>将起作用
(function() {
s4 = function(getSection) {
var self = {
name: function(getname) {
console.log("Name of the Student " + getname);
return this;
},
subject: function(getsub) {
console.log(" & Studying " + getsub);
return this;
}
}
return self;
}
})();
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Student Details</title>
</head>
<body>
<div class="hello">
<h1>Student Details</h1>
</div>
<script src="script.js"></script>
<script>
s4("5th").name("John"); //This works fine
s4("5th").subject("Python"); //This works fine
s4("5th").name("John").subject("Python"); //Shows error: SCRIPT5007: Unable to get property 'subject' of undefined or null reference
</script>
</body>
</html>
(函数(){
s4=功能(getSection){
var self={
名称:函数(getname){
log(“学生姓名”+getname);
归还这个;
},
主题:功能(getsub){
console.log(“&studing”+getsub);
归还这个;
}
}
回归自我;
}
})();
学生资料
学生资料
s4(“第五”)。姓名(“约翰”)//这个很好用
s4(“第五”)。主题(“Python”)//这个很好用
s4(“第五”)。名称(“约翰”)。主题(“Python”)//显示错误:SCRIPT5007:无法获取未定义或空引用的属性“subject”
现在,调用函数subject()
时不会出现错误,您的代码应该是
(function() {
s4 = function(getSection) {
var self = {
name: function(getname) {
console.log("Name of the Student " + getname);
return this;
},
subject: function(getsub) {
console.log(" & Studying " + getsub);
return this;
}
}
return self;
}
})();
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Student Details</title>
</head>
<body>
<div class="hello">
<h1>Student Details</h1>
</div>
<script src="script.js"></script>
<script>
s4("5th").name("John"); //This works fine
s4("5th").subject("Python"); //This works fine
s4("5th").name("John").subject("Python"); //Shows error: SCRIPT5007: Unable to get property 'subject' of undefined or null reference
</script>
</body>
</html>
(函数(){
s4=功能(getSection){
var self={
名称:函数(getname){
log(“学生姓名”+getname);
归还这个;
},
主题:功能(getsub){
console.log(“&studing”+getsub);
归还这个;
}
}
回归自我;
}
})();
学生资料
学生资料
s4(“第五”)。姓名(“约翰”)//这个很好用
s4(“第五”)。主题(“Python”)//这个很好用
s4(“第五”)。名称(“约翰”)。主题(“Python”)//显示错误:SCRIPT5007:无法获取未定义或空引用的属性“subject”
现在调用函数subject()
您的函数时不会出现错误
name: function(getname){
console.log("Name of the Student " +getname);
}
不返回此
。
将其更改为:
name: function(getname){
console.log("Name of the Student " +getname);
return this;
}
你的职能
name: function(getname){
console.log("Name of the Student " +getname);
}
不返回此
。
将其更改为:
name: function(getname){
console.log("Name of the Student " +getname);
return this;
}
因为name函数没有返回anything@jeffcarey这是正确的。Name函数没有返回任何对象,这就是为什么会得到null或未定义的引用。因为Name函数没有返回anything@jeffcarey这是正确的。Name函数没有返回任何对象,这就是为什么会得到null或未定义的引用。我们可以在调用链中的方法时连接这两个方法吗?我们可以在调用链中的方法时连接这两个方法吗