質問編集履歴

3

関数型→クラス型

2022/03/22 05:41

投稿

sgru_
sgru_

スコア133

test CHANGED
File without changes
test CHANGED
@@ -20,59 +20,55 @@
20
20
  id:""
21
21
  }
22
22
  //-----------------------------------------------------------------------------------------------------------------------------------
23
- function set(){}//set関数
23
+ class set{//set関数
24
- set.prototype = {//プロトタイプ
25
- body:function(){//body
24
+ body(){
26
25
  system["id"]="<<body>>";
27
26
  return this;
28
- },
27
+ }
29
- title:function(text) {//title
28
+ title(text){
30
29
  document.title=text;
31
30
  return this;
32
- },
31
+ }
33
- icon:function(url){//icon
32
+ icon(url){
34
33
  const new_ele = document.createElement("link");
35
34
  new_ele.rel="icon";
36
35
  new_ele.href=url;
37
36
  document.head.appendChild(new_ele);
38
37
  return this;
39
- },
38
+ }
40
- css:function(name,option){//css変更
39
+ css(name,option){
41
40
  let key_name = [];
42
41
  let key_option = [];
43
42
  if(system["id"]!==""){
44
43
  let id = undefined;
45
- if(system["id"]==="<<body>>"){
44
+ if(system["id"]==="<<body>>")id = document.body;
46
- id = document.body;
47
- }else{
45
+ else
48
46
  id = document.getElementById(system["id"]);
47
+ if((typeof name)==="object"){
48
+ key_name=[...(Object.keys(name))]
49
+ for(let i = 0;i<key_name.length;i++){
50
+ key_option.push(name[key_name[i]]);
49
- }
51
+ }
50
- if((typeof name)==="object"){
52
+ }else{
51
- key_name=[...(Object.keys(name))]
53
+ key_name.push(name);
54
+ key_option.push(option);
55
+ }
52
56
  for(let i = 0;i<key_name.length;i++){
53
- key_option.push(name[key_name[i]]);
57
+ css_edit(key_name[i],id,key_option[i]);
54
58
  }
55
- }else{
56
- key_name.push(name);
57
- key_option.push(option);
58
- }
59
- for(let i = 0;i<key_name.length;i++){
60
- css_edit(key_name[i],id,key_option[i]);
61
- }
62
- key_name=[];
59
+ key_name=[];
63
- return this;
60
+ return this;
64
61
  }else{
65
62
  console.error("エラー:\nidが指定されていません");
66
63
  }
67
- },
64
+ }
68
- change:function(text){//モードチェンジ
65
+ change(text){
69
66
  return $(text);
70
67
  }
71
68
  }
72
69
  //-----------------------------------------------------------------------------------------------------------------------------------
73
- function add(){}//add関数
70
+ class add{
74
- add.prototype = {//プロトタイプ
75
- canvas:function(sizex,sizey,id,color){//canvas作成
71
+ canvas(sizex,sizey,id,color){
76
72
  const new_ele = document.createElement("canvas");
77
73
  new_ele.style.backgroundColor=color;
78
74
  new_ele.width=sizex;
@@ -81,62 +77,61 @@
81
77
  system["id"]=id;
82
78
  document.body.appendChild(new_ele);
83
79
  return this;
84
- },
80
+ }
85
- p:function(text,id){//p作成
81
+ p(text,id){
86
82
  const new_ele = document.createElement("p");
87
83
  new_ele.id=id;
88
84
  new_ele.appendChild(document.createTextNode(text));
89
85
  document.body.appendChild(new_ele);
90
- },
86
+ }
91
- h1:function(text,id){//h1作成
87
+ h1(text,id){
92
88
  const new_ele = document.createElement("h1");
93
89
  new_ele.id=id;
94
90
  new_ele.appendChild(document.createTextNode(text));
95
- document.body.appendChild(new_ele);
91
+ document.body.appendChild(new_ele);
96
- },
92
+ }
97
- h2:function(text,id){//h2作成
93
+ h2(text,id){
98
94
  const new_ele = document.createElement("h2");
99
95
  new_ele.id=id;
100
96
  new_ele.appendChild(document.createTextNode(text));
101
- document.body.appendChild(new_ele);
97
+ document.body.appendChild(new_ele);
102
- },
98
+ }
103
- h3:function(text,id){//h3作成
99
+ h3(text,id){
104
100
  const new_ele = document.createElement("h3");
105
101
  new_ele.id=id;
106
102
  new_ele.appendChild(document.createTextNode(text));
107
- document.body.appendChild(new_ele);
103
+ document.body.appendChild(new_ele);
108
- },
104
+ }
109
- h4:function(text,id){//h4作成
105
+ h4(text,id){
110
106
  const new_ele = document.createElement("h4");
111
107
  new_ele.id=id;
112
108
  new_ele.appendChild(document.createTextNode(text));
113
- document.body.appendChild(new_ele);
109
+ document.body.appendChild(new_ele);
114
- },
110
+ }
115
- h5:function(text,id){//h5作成
111
+ h5(text,id){
116
112
  const new_ele = document.createElement("h5");
117
113
  new_ele.id=id;
118
114
  new_ele.appendChild(document.createTextNode(text));
119
- document.body.appendChild(new_ele);
115
+ document.body.appendChild(new_ele);
120
- },
116
+ }
121
- h6:function(text,id){//h6作成
117
+ h6(text,id){
122
118
  const new_ele = document.createElement("h6");
123
119
  new_ele.id=id;
124
120
  new_ele.appendChild(document.createTextNode(text));
125
- document.body.appendChild(new_ele);
121
+ document.body.appendChild(new_ele);
126
- },
122
+ }
127
- change:function(text){//モードチェンジ
123
+ change(text){
128
124
  return $(text);
129
125
  }
130
126
  }
131
127
  //-----------------------------------------------------------------------------------------------------------------------------------
132
- function remove(){}//remove関数
128
+ class remove{//remove関数
133
- remove.prototype = {//プロトタイプ
134
- id:function(id){//id指定のもの削除
129
+ id(id){
135
130
  let element = document.getElementById(id);
136
131
  element.remove();
137
132
  return this;
138
- },
133
+ }
139
- change:function(text){//モードチェンジ
134
+ change(){
140
135
  return $(text);
141
136
  }
142
137
  }

2

エラー文の提示

2022/03/22 05:19

投稿

sgru_
sgru_

スコア133

test CHANGED
File without changes
test CHANGED
@@ -185,3 +185,5 @@
185
185
  });
186
186
  });
187
187
  ```
188
+ エラー文はこれです:
189
+ Uncaught TypeError: Cannot read properties of undefined (reading 'change')

1

書式の改善

2022/03/22 05:17

投稿

sgru_
sgru_

スコア133

test CHANGED
@@ -1 +1 @@
1
- javascriptのlibraryをつくっているときのエラーの対処法がわかりません
1
+ javascriptのlibraryをつくっているときのエラー
test CHANGED
@@ -166,8 +166,7 @@
166
166
  "margin":"0",
167
167
  "padding":"0"
168
168
  });
169
- $("%add").h1("タイトル","h1");
169
+ $("%add").h1("タイトル","h1").change("%set").css({
170
- $("%set").css({
171
170
  "color":"#000000",
172
171
  "text-align":"center",
173
172
  "font-size":"20"