Skip to content
View in the app

A better way to browse. Learn more.

Web Designer Forum

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

Object oriented javascript help

Featured Replies

Hi everyone

 

Havent posted in a long time. First thank you all for the fantastic support you have given me ever sine i came here. Ok i need a little help.

 

The OO is suppose to show a number for the article, a header title and a story but havent been able to get it to work

 

I have been working on an OO that looks like this:

 

Please help

 

 

function myArticle(storyId, header, story)
{

this.storyId = storyId;
this.header = header;
this.story = story;

var newNum = document.createElement("i");
var add_storyId = document.createTextNode(this.storyId);
var newHeader = document.createElement("h2");
var add_header = document.createTextNode(this.header);
var newPara = document.createElement("p");
var add_story = document.createTextNode(this.story);
var getFooter = document.getElementById("footer");
document.body.insertBefore(Num,Story ,newPara, getFooter);

}

function init()
{

myNews = new storyId("1");
myNews = new header("The latest ne.");
myNews = new story("The lwqdqqwd ewecew3 3332edew");
myNews.show();


}

 

 

 

Im not too sure exactly what you"re trying to acheive here. On a quick inspection i can't see anywhere in your code where you call the myArticle() constructor function.

 

I know its not strictly necessary but all the DOM manipulation you have going on there would be simpler to impliment using jQuery.

Edited by rbrtsmith

Try this

 

 function Article(storyId, header, story)
     {
    
     this.storyId = storyId;
     this.header = header;
     this.story = story;   
    
    }
    
    function init()
    {    
         var myArticle = new Article("1","The latest ne.","The lwqdqqwd ewecew3 3332edew")
         console.log(myArticle);      
    }

 

A few things..

 

Classes, i.e. Article, should have a capital as the first character, and drop the 'my', so myArticle -> Article

 

By all means, when you create an instance of that object, by all means call it myArticle, see above for an example.

 

Maybe its just been my experience but I would rewrite

 

var newNum = document.createElement("i");

as

 

var newNum = function(){document.createElement("i");}

and call it via

myArticle.newNum()

 

This should get you started, constructing the object was your first, main problem

Edited by pilgrim_fgau

Create an account or sign in to comment

Account

Navigation

Search

Search

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.