js刪除table指定行「js對象的方法有哪些」

一、JavaScript的對象創建的方法

JavaScript可以直接創建對象,只須用 {}:

let javaScriptObject = {};
let testArray = [1, 2, 3, 4];

javaScriptObject.array = testArray;
console.log(javaScriptObject); /// {array: [1,2,3,4]}/

javaScriptObject.title = "Algorithms";
console.log(javaScriptObject); // { array: [ 1, 2, 3, 4 ], title: 'Algorithms' }

二、原型繼承Prototypal-Inheritance

創建的方式也簡單:

function ExampleClass() {
  this.name = "JavaScript";
  this.sayName = function () {
    console.log(this.name);
  };
} ///new object/

var example1 = new ExampleClass();
example1.sayName(); ///"JavaScript"/

也可以用添加原型的方式:

function ExampleClass(){
     this.array = [1,2,3,4,5];
     this.name = "JavaScript";
 }

 ///new object/
 var example1 = new ExampleClass();

 ExampleClass.prototype.sayName = function() {
     console.log(this.name);
 }

 example1.sayName(); ///"JavaScript"/

三、構造器與變數

建設構造器和變數:

function ExampleClass(name, size){
     this.name = name;
     this.size = size;
 }

var example = new ExampleClass("Public",5);
 console.log(example); /// {name:"Public", size: 5}/

 /// accessing public variables/
 console.log(example.name); /// "Public"/
 console.log(example.size); /// 5/

也可以仿製一個privat-property.

function ExampleClass(name, size) {
     var privateName = name;
     var privateSize = size;

     this.getName = function() {return privateName;}
     this.setName = function(name) {privateName = name;}

     this.getSize = function() {return privateSize;}
     this.setSize = function(size) {privateSize = size;}
 }

 var example = new ExampleClass("Sammie",3);
 example.setSize(12);
 console.log(example.privateName); /// undefined/
 console.log(example.getName()); /// "Sammie"/
 console.log(example.size); /// undefined/
 console.log(example.getSize()); /// 3/

原創文章,作者:投稿專員,如若轉載,請註明出處:https://www.506064.com/zh-tw/n/253046.html

(0)
打賞 微信掃一掃 微信掃一掃 支付寶掃一掃 支付寶掃一掃
投稿專員的頭像投稿專員
上一篇 2024-12-14 02:24
下一篇 2024-12-14 02:25

相關推薦

發表回復

登錄後才能評論