Hướng dẫn javascript to combine first name and last name and display full name. - javascript để kết hợp tên và họ và hiển thị tên đầy đủ.

3

Mới! Lưu câu hỏi hoặc câu trả lời và sắp xếp nội dung yêu thích của bạn. Tìm hiểu thêm.
Learn more.

**"error"
"TypeError: Person.fname is not a function
    at Person.fullname (hovaqedile.js:14:17)
    at hovaqedile.js:19:40
    at https://static.jsbin.com/js/prod/runner-4.1.4.min.js:1:13924
    at https://static.jsbin.com/js/prod/runner-4.1.4.min.js:1:10866**

Chào

Làm thế nào để kết hợp tên và họ trong JavaScript? Tôi đang gặp lỗi

function Person(){

}

Person.prototype.fname=function(){
  return "abc"
}

Person.prototype.lname=function(){
  return "lop"
}

Person.prototype.fullname=function(){
  return Person.fname() + Person.lname()
}

var p = new Person();

console.log(p.fullname())

Bạn có thể vui lòng cho biết tôi sẽ in tên đầu tiên và tên cuối cùng trong javascript

Đã hỏi ngày 16 tháng 5 năm 2018 lúc 6:40May 16, 2018 at 6:40

Hướng dẫn javascript to combine first name and last name and display full name. - javascript để kết hợp tên và họ và hiển thị tên đầy đủ.

Bạn nên sử dụng từ khóa this là một tham chiếu đến đối tượng hiện tại.reference to the current object.

function Person(){

}

Person.prototype.fname=function(){
  return "abc"
}

Person.prototype.lname=function(){
  return "lop"
}

Person.prototype.fullname=function(){
  return this.fname() + this.lname()
}

var p = new Person();

console.log(p.fullname())

Đã trả lời ngày 16 tháng 5 năm 2018 lúc 6:42May 16, 2018 at 6:42

Hướng dẫn javascript to combine first name and last name and display full name. - javascript để kết hợp tên và họ và hiển thị tên đầy đủ.

2

Bạn cần sử dụng this khi sử dụng cấu trúc

function Person(){

}

Person.prototype.fname=function(){
  return "abc"
}

Person.prototype.lname=function(){
  return "lop"
}

Person.prototype.fullname=function(){
  return Person.fname() + Person.lname()
}

var p = new Person();

console.log(p.fullname())
1 trong hàm. Điều này là do this sẽ lấy phạm vi của toàn bộ hàm
function Person(){

}

Person.prototype.fname=function(){
  return "abc"
}

Person.prototype.lname=function(){
  return "lop"
}

Person.prototype.fullname=function(){
  return Person.fname() + Person.lname()
}

var p = new Person();

console.log(p.fullname())
3 trong khi sử dụng cấu trúc
function Person(){

}

Person.prototype.fname=function(){
  return "abc"
}

Person.prototype.lname=function(){
  return "lop"
}

Person.prototype.fullname=function(){
  return Person.fname() + Person.lname()
}

var p = new Person();

console.log(p.fullname())
1. Ngoài ra, sử dụng khoảng trắng giữa
function Person(){

}

Person.prototype.fname=function(){
  return "abc"
}

Person.prototype.lname=function(){
  return "lop"
}

Person.prototype.fullname=function(){
  return Person.fname() + Person.lname()
}

var p = new Person();

console.log(p.fullname())
5 và
function Person(){

}

Person.prototype.fname=function(){
  return "abc"
}

Person.prototype.lname=function(){
  return "lop"
}

Person.prototype.fullname=function(){
  return Person.fname() + Person.lname()
}

var p = new Person();

console.log(p.fullname())
6 của bạn.

function Person(){

}

Person.prototype.fname=function(){
  return "abc"
}

Person.prototype.lname=function(){
  return "lop"
}

Person.prototype.fullname=function(){
  return this.fname() + ' '+  this.lname()
}

var p = new Person();

console.log(p.fullname())

Đã trả lời ngày 16 tháng 5 năm 2018 lúc 6:45May 16, 2018 at 6:45

Hướng dẫn javascript to combine first name and last name and display full name. - javascript để kết hợp tên và họ và hiển thị tên đầy đủ.

Ankit Agarwalankit AgarwalAnkit Agarwal

30K5 Huy hiệu vàng36 Huy hiệu bạc59 Huy hiệu đồng5 gold badges36 silver badges59 bronze badges

Sử dụng this. Tôi sẽ đọc sâu hơn về nó, vì nó là một phần cực kỳ hữu ích của ngôn ngữ. Hãy xem trang MDN trên nó.

Trong mã của bạn:

Person.prototype.fullname = function() {
  return this.fname() + this.lname();
}

Đã trả lời ngày 16 tháng 5 năm 2018 lúc 6:41May 16, 2018 at 6:41

Hướng dẫn javascript to combine first name and last name and display full name. - javascript để kết hợp tên và họ và hiển thị tên đầy đủ.

Ian Paschalian PaschalIan Paschal

6824 Huy hiệu bạc13 Huy hiệu đồng4 silver badges13 bronze badges

var Person = {
      firstName: ‘John’,
      lastName: ‘Smith’,
      fullName: function (){
          this.fullName = this.firstName 
          + ‘ ‘ + this.lastName;
      }
};
Person.fullName();
console.log(Person.fullName);

2

function Person(){

}

Person.prototype.fname=function(){
  return "abc"
}

Person.prototype.lname=function(){
  return "lop"
}

Person.prototype.fullname=function(){
  return Person.fname() + Person.lname()
}

var p = new Person();

console.log(p.fullname())
8 không phải là một hàm vì bạn phải tham khảo phiên bản khi gọi hàm của thể hiện.
function Person(){

}

Person.prototype.fname=function(){
  return "abc"
}

Person.prototype.lname=function(){
  return "lop"
}

Person.prototype.fullname=function(){
  return Person.fname() + Person.lname()
}

var p = new Person();

console.log(p.fullname())
3 là nguyên mẫu. Bạn có thể làm điều đó bằng cách sử dụng this (đề cập đến thể hiện trong trường hợp này -> thêm thông tin ở đây) thay vì
function Person(){

}

Person.prototype.fname=function(){
  return "abc"
}

Person.prototype.lname=function(){
  return "lop"
}

Person.prototype.fullname=function(){
  return Person.fname() + Person.lname()
}

var p = new Person();

console.log(p.fullname())
3

Person.prototype.fullname = function() {
  return this.fname() + this.lname();
}

Đã trả lời ngày 16 tháng 5 năm 2018 lúc 6:46May 16, 2018 at 6:46

function addFullNameProperty(obj) {
  
   obj.fullName = obj.firstName + ' ' + obj.lastName;
  
  return obj.fullName;

}

Hướng dẫn javascript to combine first name and last name and display full name. - javascript để kết hợp tên và họ và hiển thị tên đầy đủ.

Cigien

56.1K11 Huy hiệu vàng65 Huy hiệu bạc103 Huy hiệu đồng11 gold badges65 silver badges103 bronze badges

Đã trả lời ngày 6 tháng 3 lúc 17:35Mar 6 at 17:35

1

Tôi nghĩ rằng đây có thể là giải pháp dễ nhất.

    const firstName = 'Amir';
    const lastName = 'Khan';
    function fullName(firstName,lastName){
        return firstName.concat(' ').concat(lastName);
    }
    console.log(fullName(firstName,lastName));

Đã trả lời ngày 27 tháng 9 lúc 8:20Sep 27 at 8:20

Hướng dẫn javascript to combine first name and last name and display full name. - javascript để kết hợp tên và họ và hiển thị tên đầy đủ.

Làm thế nào để bạn viết tên và họ trong JavaScript?

Có: var fullname = "Paul Steve Panakkal". chia (''), firstName = fullName [0], lastName = fullName [fullName.length - 1];split(' '), firstName = fullName[0], lastName = fullName[fullName. length - 1];

Làm thế nào tôi có thể viết tên của tôi trong JavaScript?

JavaScript, chức năng, tự hào tên tôi (người mới bắt đầu)..
Viết một hàm gọi là Namestring ().
Nó nên lấy tên làm tham số ..
Hàm trả về một chuỗi bằng "Hi, tôi là" + "" " + tên ..
Gọi đặt tên () bằng cách chuyển tên của bạn và sử dụng bảng điều khiển. Đăng nhập để in đầu ra ..

Tên .Name trong JavaScript là gì?

Thuộc tính tên được sử dụng để xác định dữ liệu biểu mẫu sau khi nó đã được gửi đến máy chủ hoặc để tham chiếu dữ liệu biểu mẫu bằng cách sử dụng JavaScript ở phía máy khách.Lưu ý: Chỉ các thành phần biểu mẫu có thuộc tính tên sẽ có giá trị của chúng được thông qua khi gửi biểu mẫu.used to identify form data after it has been submitted to the server, or to reference form data using JavaScript on the client side. Note: Only form elements with a name attribute will have their values passed when submitting a form.

Làm cách nào để kết hợp tên đầu tiên và họ trong Oracle?

Đối với Oracle, đó là: Chọn FirstName+''+LastName từ EMP;hoặc chọn tên Concat (FirstName, LastName) từ EMP;(Theo như tôi nhớ Concat trong Oracle không thể mất nhiều hơn hai đối số).Vì vậy, nếu bạn muốn kết hợp nhiều hơn hai trường, tốt hơn là sử dụng toán tử nối.