欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

js对象的方法有哪些(js删除table指定行)

程序员文章站 2023-11-27 16:48:16
一、javascript的对象创建的方法javascript可以直接创建对象,只须用 {}:let javascriptobject = {};let testarray = [1, 2, 3...

一、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/